求一条SQL语句

claymore1114 2009-12-24 01:08:18
有User表、Topic表,
要求:
返回一个列表,包含 用户姓名(name),用户id,帖子名称title,内容Content。(规则是在1小时内发帖最多排名靠前)
...全文
140 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
claymore1114 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 caishasha 的回复:]
参考:
http://bbs.itzcn.com/thread-938-1-1.html

[/Quote]
只取出了 用户表啊 还有帖子表没出来
claymore1114 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wufeng4552 的回复:]
引用 8 楼 claymore1114 的回复:
表结构如下:
User表 :ID UserName,PassWord
Topic表:ID,UserID,Title,Content,AddTime

SQL codeselect username,
userid,
Title,
Contentfrom[User] a,Topic bwhere a.id=b.useridorderbyisnull((selectcount(*)from Topicwhere UserID=b.UserID ),0)
[/Quote]
按用户ID分组 怎么办
hbqhd 2009-12-24
  • 打赏
  • 举报
回复

select c.userid,d.username,c.title,c.content
from
(select row_number() over(order by topiccount desc) as rank,userid
from
(select userid,count(*) as topiccount from topic group by userid where addtime<adddate(dd,getdate(),1)) a) b
inner join topic c on b.userid=c.user.id
left outer join user d on b.user_id=d.userid
order by b.rank
水族杰纶 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 claymore1114 的回复:]
表结构如下:
User表 :ID UserName,PassWord
Topic表:ID,UserID,Title,Content,AddTime
[/Quote]
select username,
userid,
Title,
Content
from [User] a,Topic b
where a.id=b.userid
order by isnull((select count(*) from Topic where UserID=b.UserID ),0)
claymore1114 2009-12-24
  • 打赏
  • 举报
回复
表结构如下:
User表 :ID UserName,PassWord
Topic表:ID,UserID,Title,Content,AddTime
ACMAIN_CHM 2009-12-24
  • 打赏
  • 举报
回复
你的表中连时间字段都没有,怎么进行判断。
ACMAIN_CHM 2009-12-24
  • 打赏
  • 举报
回复
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

gcw633 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 claymore1114 的回复:]
引用 1 楼 aimee_99 的回复:
select name,userid,title,content
from user a,topic b
where a.usrid = b.userid
order by (select count(1) from topic where userid = a.userid) desc

我的这个要求怎么弄
[/Quote]
就是说user表中有些什么字段,topic表中有些什么字段的意思
claymore1114 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aimee_99 的回复:]
select name,userid,title,content
from user a,topic b
where a.usrid = b.userid
order by (select count(1) from topic where userid = a.userid) desc
[/Quote]
我的这个要求怎么弄
水族杰纶 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 claymore1114 的回复:]
有User表、Topic表,
要求:
返回一个列表,包含 用户姓名(name),用户id,帖子名称title,内容Content。(

规则是按用户ID分组列出改用户相关所有帖子名称和内容。顺序规则:按发帖最多用户排前)
[/Quote]
給出表結構
測試數據+規則+結果吧
claymore1114 2009-12-24
  • 打赏
  • 举报
回复
有User表、Topic表,
要求:
返回一个列表,包含 用户姓名(name),用户id,帖子名称title,内容Content。(

规则是按用户ID分组列出改用户相关所有帖子名称和内容。顺序规则:按发帖最多用户排前)
aimee_99 2009-12-24
  • 打赏
  • 举报
回复
select name,userid,title,content
from user a,topic b
where a.usrid = b.userid
order by (select count(1) from topic where userid = a.userid) desc

34,593

社区成员

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

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