日期:2014-05-18  浏览次数:20678 次

高手请看此条SQL语句
select   *   from  
(select   distinct   top   5   tid   from   table1)   b,
(select   top   5   tid,tname   from   table2  
where   tid   =   b.tid)   a

其中table2有多行数据,想把table2中属于table1的前5条记录取出来
除了用游标外,还有其它办法吗,上面是我写的,但不成功,请高手指教


------解决方案--------------------
select * from table2 where tid in (select top 5 tid from table1)
------解决方案--------------------
select b.tid,a.tid,a.tname from
(select distinct top 5 tid from table1) b,
table2 a
where a.tid = b.tid

------解决方案--------------------
select * from
(select top 5 distinct tid from table1) b--把distinct 换一下位置
where tid in
(select top 5 tid from table2 )
------解决方案--------------------
把表結構, 數據和想要的結果貼出來看看
------解决方案--------------------
select * top 5 tid,tname from table2
where tid in(select tid from table1))
------解决方案--------------------
select top 5 B.* from table1 A inner join table2 B on A.tid = B.tid
------解决方案--------------------
select top 5 * from table2 where tid in (select distinct tid from table1)