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)

解决方案 »

  1.   

    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.   

    问题含糊,举个例子更清晰1、union/union all
    2、join
      

  3.   

    我说清楚一些,是这样的:
    我想在一张个人信息表中,随机取出10条记录,其中3条必须是字段“Sex”中值为“男”的记录,另外7条必须是字段“Sex”中值为“女”的记录,而且前3条为“男”的记录中,必须有且只有一条记录的字段“age”大于30,后7条为“女”的记录中,必须有且只有两条记录的字段“age”大于30,请问这样的语句要怎么写呢,谢谢各位大侠了
      

  4.   

    你的要求用 union/union all 最简单,直观!select top 1  * from table1 where sex='男' and age >30 order by newid() union allselect top 2  * from table1 where sex='男' and age<=30 order by newid() union allselect top 2  * from table1 where sex='女' and age >30 order by newid() union allselect top 5  * from table1 where sex='女' and age<=30 order by newid()
      

  5.   

    楼上的大侠,我按照你的写法写了,还是会出错,sql server的错误提示是:
    “如果语句中包含UNION运算符,那么ORDER BY 子句中的项就必须出现在选择列表中。”
    如果我把“order by newid()”该句去掉,就不会有错误了,可是我又需要随机选择,怎么办好呢?多谢了
      

  6.   

    上面写的问题:
     语句中包含 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
      

  7.   

    非常感谢 hglhyy!!!!!!!!!!!用你的方法,问题已经解决了,再次感谢:)