在"Select top 2 * from news order by count"进行筛选时,如果count值有重复时,则筛选结果将不是2条记录,这个问题该怎么解决?

lcq419 2006-09-19 07:39:15
在"Select top 2 * from news order by count"进行筛选时,如果count值有重复时,则筛选结果将不是2条记录,这个问题该怎么解决?

例如:
news 表的结构如下:
id title content count

假如现在表中有以下几个记录:
id title content count
1 新闻1 内容1 10
2 新闻2 内容2 8
3 新闻3 内容3 6
4 新闻4 内容4 9
5 新闻5 内容5 10
6 新闻6 内容6 10

则执行:Select top 2 * from news order by count
后执行结果如下:
id title content count
1 新闻1 内容1 10
5 新闻5 内容5 10
6 新闻6 内容6 10
4 新闻4 内容4 9

而我想得到的结果是:
id title content count
1 新闻1 内容1 10
4 新闻4 内容4 9

请大家帮我想想办法该怎么解决这个问题
...全文
242 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcq419 2006-09-20
  • 打赏
  • 举报
回复
Select top 2 * from news A
where not exists(select * from news where count=A.count and id<A.id)
order by count desc

这样的话,经过验证,已经解决问题了,谢谢zsforever(虎虎)!
zsforever 2006-09-19
  • 打赏
  • 举报
回复
Select top 2 * from news A
where not exists(select 1 from news where count=A.count and id<A.id)
order by count desc

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧