请教sql查询怎么忽略重复行

liushihua 2008-12-18 09:49:52
做了个小bbs
要在bbs回帖表(bbs_post) 中查询10个最新的ThreadId(主帖表的外键)
select top 10 id,threadId,pubdate from bbs_post order by pubdate
查询结果如下:
id threadId pubdate
17 34 2008-12-15
18 20 2008-12-15
19 20 2008-12-15
。。。。。
。。。。
请教各位,怎么忽略或跳过重复的记录而找到最新的10条呢?
18 20
19 20
...全文
395 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
criedshy 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 liushihua 的回复:]
criedshy 说的行我又加了group by threadId 分组 不然报错
select top 10 t.threadId,t.pubdate
from (select threadId,max(pubdate)
as pubdate from bbs_post group by threadId) t order by pubdate desc
结果OK:
threadId pubdate
20 2008-12-15 23:17:06.000
76 2008-12-15 22:17:06.763
89 2008-12-15 22:17:06.763
45 2008-12-15 22:17:06.763
56 2008-12-15 22:17:06.763
34 2008-12-15 …
[/Quote]

是啊,刚才忘了加group by,哈哈
CluoLeo 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 criedshy 的回复:]
SQL codeselect top 10 t.threadId,t.pubdate
from (select threadId,max(pubdate) as pubdate from bbs_post) t order by pubdate
[/Quote]
这个方法我也认同
敏捷William 2008-12-18
  • 打赏
  • 举报
回复
我的问题也OK了!!
liushihua 2008-12-18
  • 打赏
  • 举报
回复
criedshy 说的行我又加了group by threadId 分组 不然报错
select top 10 t.threadId,t.pubdate
from (select threadId,max(pubdate)
as pubdate from bbs_post group by threadId) t order by pubdate desc
结果OK:
threadId pubdate
20 2008-12-15 23:17:06.000
76 2008-12-15 22:17:06.763
89 2008-12-15 22:17:06.763
45 2008-12-15 22:17:06.763
56 2008-12-15 22:17:06.763
34 2008-12-15 22:17:06.763
23 2008-12-15 22:17:06.750
22 2008-12-15 22:17:06.750
65 2008-12-15 22:17:06.750
24 2008-12-15 22:17:06.750
criedshy 2008-12-18
  • 打赏
  • 举报
回复
希望有对你帮助
criedshy 2008-12-18
  • 打赏
  • 举报
回复
select top 10 t.threadId,t.pubdate 
from (select threadId,max(pubdate) as pubdate from bbs_post) t order by pubdate
criedshy 2008-12-18
  • 打赏
  • 举报
回复
select top 10 t.threadId,t.pubdate 
from (select threadId,max(pubdate) as pubdate from bbs_post) t order by pubdate
criedshy 2008-12-18
  • 打赏
  • 举报
回复
按照ThreadID 分组,取最大时间就不得了
liushihua 2008-12-18
  • 打赏
  • 举报
回复
我是这样用的
select distinct top 10 threadId,pubdate from bbs_post order by pubdate desc

结果:
id pubdate
20 2008-12-15 23:17:06.000
20 2008-12-15 22:17:07.623
20 2008-12-15 22:17:07.610
20 2008-12-15 22:17:07.593
20 2008-12-15 22:17:07.577
20 2008-12-15 22:17:07.560
20 2008-12-15 22:17:07.547
20 2008-12-15 22:17:07.530
20 2008-12-15 22:17:07.513
20 2008-12-15 22:17:07.500
id 全是重复的
yagebu1983 2008-12-18
  • 打赏
  • 举报
回复
distinct!!!
关键你是怎样用!!
zjybushiren88888 2008-12-18
  • 打赏
  • 举报
回复
distinct关键字 去除重复
liushihua 2008-12-18
  • 打赏
  • 举报
回复
我想要这样的结果:
ThreadID pudate
65 2008-12-15 22:17:07.750
25 2008-12-15 22:17:06.750
39 2008-12-15 22:17:05.750
27 2008-12-15 22:17:04.750
29 2008-12-15 22:16:06.750
22 2008-12-15 22:13:06.750
43 2008-12-15 22:11:06.750
24 2008-12-15 22:05:06.750
26 2008-12-15 22:13:06.750
33 2008-12-15 22:11:06.750
pudate是desc排序的
HolyPlace 2008-12-18
  • 打赏
  • 举报
回复
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
参考一下。
敏捷William 2008-12-18
  • 打赏
  • 举报
回复
select distinct top 10 threadId,pubdate from bbs_post order by pubdate

这样查询出时间并不是最新的啊?

如果是用desc倒叙后,编号就不对了??
liushihua 2008-12-18
  • 打赏
  • 举报
回复
select distinct top 10 threadId,pubdate from bbs_post order by pubdate desc
结果:
20 2008-12-15 23:17:06.000
20 2008-12-15 22:17:07.623
20 2008-12-15 22:17:07.610
20 2008-12-15 22:17:07.593
20 2008-12-15 22:17:07.577
20 2008-12-15 22:17:07.560
20 2008-12-15 22:17:07.547
20 2008-12-15 22:17:07.530
20 2008-12-15 22:17:07.513
20 2008-12-15 22:17:07.500
zetee 2008-12-18
  • 打赏
  • 举报
回复
distinct
dd__dd 2008-12-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 koukoujiayi 的回复:]
id不要显示,可这样:
select distinct top 10 threadId,pubdate from bbs_post order by pubdate
[/Quote]
正解,因为id是唯一的所以要用distinct就必须不显示id
jiang_jiajia10 2008-12-18
  • 打赏
  • 举报
回复
用 distinct
koukoujiayi 2008-12-18
  • 打赏
  • 举报
回复
id不要显示,可这样:
select distinct top 10 threadId,pubdate from bbs_post order by pubdate
敏捷William 2008-12-18
  • 打赏
  • 举报
回复
这个问题我也遇到过,不知道该怎么解决?
希望你的有人解决,我也好看看。

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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