请教一个统计沙发数的MySql语句

taito 2011-05-16 03:26:25
topic
=================
id
title

comments
================
topic_id <= 关联topic 的id
user_id <= 关联用户

user
================
id
name


想得到对所有topic发表评伦(comments)的、第一个用户进行统计(沙发),并做一个排序。

比如
========================
姓名 沙发数 topic_id
张三 12 1,3,5,12,33...
李四 5 2,4,6,8,10
...


虚心求教!
...全文
121 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
taito 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acmain_chm 的回复:]

楼主是如何来识别哪个评论是第一个呢?

(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的……
[/Quote]

不错的建议,下次采用,多谢
ACMAIN_CHM 2011-05-16
  • 打赏
  • 举报
回复
楼主是如何来识别哪个评论是第一个呢?

(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)

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

gungod 2011-05-16
  • 打赏
  • 举报
回复
1.取comments表相同topic_id值的第1个user_id值;
2.再根据user_id值分组,求和,关联用户名表;
rucypli 2011-05-16
  • 打赏
  • 举报
回复
假设rowdate为comments表的时间列
select T.user_id '姓名',count(*) '沙发数',group_concat(T.topic_id) 'topic_id'
from (
select *
from comments A
where not exists (select 1 from comments where A.rowdate<rowdate and A.topic_id=topic_id)
)T
group by T.user_id
wwwwb 2011-05-16
  • 打赏
  • 举报
回复
select a.id,a.name,count(*),group_concat(b.topic_id) from user a inner join comments b on a.user_id=b.user_id
inner join topic c on c.id=b.topic_id
group by a.id,a.name
wwwwb 2011-05-16
  • 打赏
  • 举报
回复
贴建表及插入记录的SQL,及要求结果出来看看

select a.name,count(*),group_concat(b.topic_id) from user a inner join comments b on a.user_id=b.user_id
inner join topic c on c.id=b.topic_id

56,940

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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