一个sql语句问题

tel7758 2006-07-25 05:15:05
我想实现一个sql语句功能,就是选出数据库中前 10条不重复userid的记录 select top 10 distinct userid,id,title from table这样写为什么提法不对呢。如果要实现此功能要怎么实现呀~~~`用sql语句
...全文
188 14 打赏 收藏 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tel7758 2006-07-26
  • 打赏
  • 举报
回复
结贴
tel7758 2006-07-26
  • 打赏
  • 举报
回复
最终解决方法:这个是最好的select top 10 userid,max(id) as id,max(title) as title from table
smartcatiboy 2006-07-25
  • 打赏
  • 举报
回复
justcode() 的办法好,学习。
小码 2006-07-25
  • 打赏
  • 举报
回复
不太懂你那表结构,如果我没理解错,你试试:select top 10 userid,id,title from table where userid in(select top 10 distinct userid from table order by id desc) order by id desc
smartcatiboy 2006-07-25
  • 打赏
  • 举报
回复
starsword(星语星剑) ,你这一句
select top 1 id from table where userid = a.userid order by id
里面userid=a.userid查找的时候会不会恒等?
smartcatiboy 2006-07-25
  • 打赏
  • 举报
回复
userID 是主键
直接查就行 select Top 10 from TABLE Order by ID

userID 不是主键就

oRS.open "select * from TABLE Order by ID",oConn,1,1

dim i,iPreID

i=0
iPreID=0

Do while not(oRs.eof)
if i<10 then
'如果id相同就del recordset,但不更新数据库
while iPreID=oRS("ID")
rs.delete
oRs.MoveNext
wend
iPreID=oRs("ID")
i=i+1
else
'超过10条后一律删掉
rs.delete
end if
loop
starsword 2006-07-25
  • 打赏
  • 举报
回复
再多说一句,最后一个 order by userid 你可以改成其它你想要的排序方式
反正 select top 10 * from table a where id in (select top 1 id from table where userid = a.userid order by id) 已经实现了每个 userid 取一条记录,然后取按什么排序的前10条,你自己看着办吧
starsword 2006-07-25
  • 打赏
  • 举报
回复
I 服了 you

select top 10 * from table a where id in (select top 1 id from table where userid = a.userid order by id) order by userid

按userid排序,每个userid取第一个id的记录,共取10个不同userid的记录,这样行了吧?
tel7758 2006-07-25
  • 打赏
  • 举报
回复
在线等。急。。。。。
tel7758 2006-07-25
  • 打赏
  • 举报
回复
高手出来看看呀~~`
tel7758 2006-07-25
  • 打赏
  • 举报
回复
这个例子中的userid不是主键呀。大家是不是都当成主键了呀,按上面大家的方面都不行呀。还会有重复的,声明:select top 10 distinct userid,id,title from table这样写是不对的。。。上面两们朋友都回答错了,而vitor_lt() 朋友回答的是对了。可是,是不是把userid当成主键了呀?按上面vitor_lt() 朋友方法。还是能选出重复userid的记录来
starsword 2006-07-25
  • 打赏
  • 举报
回复
select top 10 distinct userid,id,title from table

你把 distinct 放到 top 10 前面试一下
若白师妹 2006-07-25
  • 打赏
  • 举报
回复
select distinct top 10 userid,id,title from table
vitor_lt 2006-07-25
  • 打赏
  • 举报
回复
分组查询,可得到前10条的ID
select top 10 userid from table group by userid

如果要得到前10条所有字段就用下边这条
select * from table where userid in
(select top 10 userid from table group by userid )


下边是我用MS-SQL的Northwind数据库做的试验,你可以看一下结果
select top 10 customerid from Customers group by customerid

select * from Customers where customerid in
(select top 10 customerid from Customers group by customerid)

28,402

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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