1)select a.*,b.* from (select .... from a )a inner join (select * from b )b on a.fid = b.fid
2)select * from (select a.*,b.* from a inner join b on a.fid = b.fid)
2)select * from (select a.*,b.* from a inner join b on a.fid = b.fid)
2)select * from (select a.*,b.* from a inner join b on a.fid = b.fid)
2、join
我想在一张个人信息表中,随机取出10条记录,其中3条必须是字段“Sex”中值为“男”的记录,另外7条必须是字段“Sex”中值为“女”的记录,而且前3条为“男”的记录中,必须有且只有一条记录的字段“age”大于30,后7条为“女”的记录中,必须有且只有两条记录的字段“age”大于30,请问这样的语句要怎么写呢,谢谢各位大侠了
“如果语句中包含UNION运算符,那么ORDER BY 子句中的项就必须出现在选择列表中。”
如果我把“order by newid()”该句去掉,就不会有错误了,可是我又需要随机选择,怎么办好呢?多谢了
语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。
改:(用临时表)select top 1 * into #t from table1 where sex='男' and age >30 order by newid()
insert into #t
select top 2 * from table1 where sex='男' and age<=30 order by newid()
insert into #t
select top 2 * from table1 where sex='女' and age >30 order by newid()
insert into #t
select top 5 * from table1 where sex='女' and age<=30 order by newid()
select * from #tdrop table #t