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

求一条SQL语句。解决办法

2012-02-13 
求一条SQL语句。表A:-----------------------------id分类类名1aaaa2bbbb3cccc---------------------------

求一条SQL语句。
表A:
-----------------------------
id     分类     类名
1         a           aaa
2         b           bbb
3         c           ccc
-----------------------------

表B:
-----------------------------
分类       数据
1               X1
1               X2
1               X3
2               Y1
2               Y2
2               Y3
3               Z1
3               Z2
3               Z3
-----------------------------

请问有没有办法用一条SQL语句从数据表   B   中取出   每个   分类的前   2   个数据?
我现在只能一次取一个分类的数据,很没效率。。。。

[解决办法]
select * from 表B as a where (id IN (select top 2 from 表B as b where b.分类=a.分类))

未测试 不知道行不行


[解决办法]
select * from B as a
where (select count(*) from B where class = a.class and data > a.data ) < 2
order by class,data DESC
[解决办法]
sql= "select * from 表A "
rs.open sql,ado,3,3
if not(rs.bof and rs.eof) then
do while not rs.eof
sql=select top 2 * from 表B
.........................
rs.movenext
loop
end if

[解决办法]
select id, A.分类, 类名, 数据 from A, (select * from B as BB where 数据 in (select top 2 数据 from B where 分类=BB.分类)) as BBB where id=BBB.分类

热点排行