bbs.ChinaUnix.net
首页 | 微博 | 新闻 | Linux | FreeBSD | AIX | Windows | 博客 | 论坛 | 存储 | 网络 | 人才 | Wiki | 资料 | 读书 | 手册 | 下载 | 空间 | 搜索
  免费注册 | 忘记密码 | 会员登录 | 搜索 | 帮助 


请教一个mysql两个表查询问题


  首页 » 论坛 » MySQL »
[打印] [收藏] [本帖文本页] [推荐此主题给朋友]

我有两个表,A和B
A表存储文章信息,B表存储对A中的文章评论信息,两个表之间通过docid来关联。现在我想按照每个文章评论数的多少来降序或升序查出文章,我该怎么做呢?

[ 本帖最后由 chsan 于 2008-4-22 15:39 编辑 ]



select b.docid,count(1) as count left join b using(docid) group by a.docid order by count desc;
__________________________________
David Yeung,
MySQL Principal Support Engineer,
Sun Gold Partner.
Forum: http://bbs.actionsky.com
Training: http://www.chinamysql.cn/
My Blog:http://yueliangdao0608.cublog.cn



回复 #2 yueliangdao0608 的帖子

谢谢版主,在你的提示下,我的问题解决了,附上最后查询语句:
select B.docid, A.*,count(1) as count from B  left join A using(docid)  group by A.docid order by count desc ;

但是现在还有个问题,那就是我在计算评论总数时有个条件,就是只计算那些已经审核了的评论,是用where语句吗,用where又应该放在什么地方呢?





QUOTE:
原帖由 chsan 于 2008-4-22 15:22 发表
谢谢版主,在你的提示下,我的问题解决了,附上最后查询语句:
select B.docid, A.*,count(1) as count from B  left join A using(docid)  group by A.docid order by count desc ;

但是现在还有个问题,那 ...



1、select B.docid, A.*,count(1) as count from B  left join A on (a.docid = b.docid and ...) group by A.docid order by count desc ;
2、select B.docid, A.*,count(1) as count from B  left join A using(docid) where ... group by A.docid order by count desc ;
__________________________________
David Yeung,
MySQL Principal Support Engineer,
Sun Gold Partner.
Forum: http://bbs.actionsky.com
Training: http://www.chinamysql.cn/
My Blog:http://yueliangdao0608.cublog.cn



回复 #4 yueliangdao0608 的帖子

非常感谢,用第二种办法解决了,辛苦了!



学习,这样可不可以?
select a.docid,t.total
from a
left join
(
select b.docid,count(*) total
from b
group by b.docid
) t1
on a.docid=t.docid
order by total;


  首页 » 论坛 » MySQL »
Copyright © 2001-2010 ChinaUnix.net All Rights Reserved     联系我们:

感谢所有关心和支持过ChinaUnix的朋友们    转载本站内容请注明原作者名及出处

京ICP证041476号


清除 Cookies - ChinaUnix - Archiver - WAP - TOP