select top 5 * from book where sort_id=1 union select top 5 * from book where sort_id=2 union select top 5 * from book where sort_id=3 union select top 5 * from book where sort_id=4 union select top 5 * from book where sort_id=5
调试欢乐多
select a.* from 表 a
where sort_id in ( select top 5 sort_id from 表 where sort_id=a.sort_id )
a.*
from
book a
where
a.title in(select top 5 title from book where sort_id=a.sort_id)
where exists ( select top 5 sort_id from book where sort_id=a.sort_id )
select 'sdfsf','1'
union select 'fghfer','1'
union select 'ooor','1'
union select 'epp','1'
union select 'e2er','1'
union select '23wer','1'
union select 'egher','1'
union select 'eyuter','5'
union select 'ejker','5'
union select 'bnrgher','2'
union select 'm,rwer','2'
union select 'm,rwgher','2'
union select 'erwer','3'
union select 'kjghwer','3'
union select 'vcvgher','3'
union select 'nbghwer','3'
union select 'ryghg','3'
union select 'yuy','4'select b.* from book b where book_id in
(select top 5 book_id from book where short_id =b.short_id)drop table book
其他SQL兄的方式我也试过,上面几个用sort_id不行的,谢谢!!
散分,结贴.
下面是我在db2中的测试成功的sql,数据才15405条,比较慢
select count(*) from (
SELECT a.id,a.comcode from tbl_person a
where a.id in (
select b.id from tbl_person b
where b.comcode = a.comcode order by b.comcode fetch first 5 rows only
) order by a.comcode
) as t group by t.comcode