+ -
当前位置:首页 → 问答吧 → 数据查询问题,join了很久都没join出来

数据查询问题,join了很久都没join出来

时间:2010-11-13

来源:互联网

表User
ID---USERNAME
1---U1
2---U2
3---U3
4---U4

表Article
ID---UserID---Title
1---1----T1
2---3---T3

表Comment
ID---ArticleID---UserID---Content
1---1---1---C1
2---1---2---C2
3---2---1---C3


Access数据库中有3个表,现需要列出所有用户写的文章(用户需全列出,不管有没有写文章),并统计该文章的评论数,如下表:
UserName---Title---CommentCount
U1---T1---2
U2---NULL---0
U3---T3---1
U4---NULL---0

应如何写SQL语句?最好能写成先把表Article和表Comment JOIN起来,再将合集和表User JOIN起来~~

作者: pxh0829   发布时间: 2010-11-13

select a.uername,b.title,count(c.[id]) as CommentCount from (([user] a left join article b on b.userid=a.[id]) left join comment c on c.articleid=b.[id] and c.userid=a.[id]) group by a.uername,b.title

以上语句未测试

这里需要说明的是ACCESS数据库在join的时候要加括号。
而且上面举例说明了括号的加法
这里还有一个更简单的示例语句
http://www.f6n.net/blog/web/20090606292.html

作者: cs99619   发布时间: 2010-11-15