数据库有个表 含有字段tid (帖子id) author (发帖或者回帖作者) first(含有数字1和0 1代表这个帖子是作者发的帖子,0代表这个帖子是回的帖子) subject(是主题 如果first=1 主题即为发帖的题名,如果first=0 主题为空) 我想要获取这个表中,帖子回复最多的帖子名称,回帖数量,以及作者名称,
我是这样做的,select count(tid) as number, tid,author,subject from cdb_posts where first=0 group by tid order by count(tid) desc limit 0,1这样只能得到帖子tid 和作者名称 subject为空 因为我限制了first=0,我该怎么改进这个sql语句才能做到 可以吧count(tid),author,subject都查询出来呢。在线等高人。
我是这样做的,select count(tid) as number, tid,author,subject from cdb_posts where first=0 group by tid order by count(tid) desc limit 0,1这样只能得到帖子tid 和作者名称 subject为空 因为我限制了first=0,我该怎么改进这个sql语句才能做到 可以吧count(tid),author,subject都查询出来呢。在线等高人。
where first=1
and tid in (select tid,count(*) as replyCnt
from cdb_posts
group by tid
limit 1
)当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
select a.*,b.replyCnt
from cdb_posts a, (select tid,count(*) as replyCnt
from cdb_posts
group by tid
limit 1
) b
where a.tid=b.tid
a.first=1