如何取得表中无重复的前10条记录

yk001 2006-09-21 05:41:23
select top 10 bc.title,bc.Author,bc.DynamicUrl,bc.StaticUrl,bc.CategoryID,bl.title as Description from blog_content bc inner join blog_linkcategories bl on bc.categoryid=bl.categoryid where posttype=1
我想取一个文章表的前10条记录 但是这10条作者不能重复 ,也就是说一个人只能有1篇文章在TOP10中 ,上面句子取到的会有重复 搞了我一下午了 头疼啊 请高手指点
...全文
141 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq18860787 2006-09-22
  • 打赏
  • 举报
回复
yk001() 你在sql上运行没啊??我可是调试后发上去的啊!!
csdnweii(庸医) 你的方法可以,但是过于繁杂.程序要的是简单,要的是效率!!
yk001 2006-09-21
  • 打赏
  • 举报
回复
受益非浅啊
csdnweii 2006-09-21
  • 打赏
  • 举报
回复
有标志列吗?
如果没有先产生个临时表,添加一个 : select identity(int,1,1) [id],title,Author, ... ,CategoryID into #temp from blog_content

然后
select top 10 bc.title,bc.Author,bc.DynamicUrl,bc.StaticUrl,bc.CategoryID,bl.title as Description from #temp bc inner join blog_linkcategories bl on bc.categoryid=bl.categoryid where posttype=1 and [id] in (select min([id]) as [id] from #temp group by Author)

如果有标志列(把[id]换成你的标志列的名称):
select top 10 bc.title,bc.Author,bc.DynamicUrl,bc.StaticUrl,bc.CategoryID,bl.title as Description from blog_content bc inner join blog_linkcategories bl on bc.categoryid=bl.categoryid where posttype=1 and [id] in (select min([id]) as [id] from blog_content group by Author)
yk001 2006-09-21
  • 打赏
  • 举报
回复
select distinct top 10 bc.Author from blog_content bc inner join blog_linkcategories bl on bc.categoryid=bl.categoryid where posttype=1
这么写就可以取到作者 不报错 可是 在bc.Author 在加上其他字段 distinct就失去作用了 但是我需要其他字段的值 该怎么办
yk001 2006-09-21
  • 打赏
  • 举报
回复
Hopewell_Go:
这么写要报错
消息156,级别15,状态1,第1 行
关键字'distinct' 附近有语法错误。
qq18860787 2006-09-21
  • 打赏
  • 举报
回复
例子:
use pubs
go
select DISTINCT top 10 city from authors
go

说明:主要是加上关键字DISTINCT可以让记录不重复出现!
yk001 2006-09-21
  • 打赏
  • 举报
回复
能不能写出来让我学习下:)
Well 2006-09-21
  • 打赏
  • 举报
回复
select top 10 distinct bc.title,bc.Author,bc.DynamicUrl,bc.StaticUrl,bc.CategoryID,bl.title as Description from blog_content bc inner join blog_linkcategories bl on bc.categoryid=bl.categoryid where posttype=1
wangwei8477 2006-09-21
  • 打赏
  • 举报
回复
写成分页存储过程,每行10条,这样行不?
yk001 2006-09-21
  • 打赏
  • 举报
回复
没人吗 写成存储过程也行哦

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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