首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

高手请看此条SQL语句解决方案

2012-02-01 
高手请看此条SQL语句select*from(selectdistincttop5tidfromtable1)b,(selecttop5tid,tnamefromtable2wher

高手请看此条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)

热点排行