如何查询一个表中,各个分类的前面2条数据,用一条sql语句

[复制链接]
查看11 | 回复3 | 2018-9-4 10:34:47 | 显示全部楼层 |阅读模式
select top2 * from 表名 where 条件1 unionselect top2 * from 表名 where 条件2 unionselect top2 * from 表名 where 条件3分类比较多的话考虑用存储过程(游标)
回复

使用道具 举报

千问 | 2018-9-4 10:34:47 | 显示全部楼层
如你所愿,一条语句完成select [name],[fl],[id] from t2 where [id] in ((select min([id])[id] from t2 group by [fl])union(select min([id])[id] from t2 where [id] not in (select min([id]) from t2 group by [fl]) group by [fl]))order by [fl],[id]
回复

使用道具 举报

千问 | 2018-9-4 10:34:47 | 显示全部楼层
select top 2 * from 表名 where f1=1;//这求出fl=1的前两条select top 2 * from 表名 where f1=2//这求出fl=2的前两条select top 2 * from 表名 where f1=1 union select top 2 * from 表名 where f1=2//求出fl=1的前两条并上fl=2的前两条//不知道我明不明白你说的意思,我感觉我理解可能有误
回复

使用道具 举报

千问 | 2018-9-4 10:34:47 | 显示全部楼层
select top2 * from 表名 where 条件1 and 条件2dsadsadada
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行