我在数据库中有以下表格:
表post
:
+---------------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------+------+-----+---------+----------------+
| post_id | int(11) | NO | PRI | NULL | auto_increment |
| post_content | varchar(50000) | NO | | NULL | |
| post_date | datetime | NO | | NULL | |
| post_summary | varchar(1000) | YES | | NULL | |
| post_title | varchar(300) | NO | | NULL | |
| post_visitors | int(11) | NO | | NULL | |
| user_id | int(11) | NO | MUL | NULL | |
| category_id | int(11) | NO | MUL | NULL | |
+---------------+----------------+------+-----+---------+----------------+
表user
:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| user_id | int(11) | NO | PRI | NULL | auto_increment |
| user_image | varchar(500) | YES | | NULL | |
| user_name | varchar(45) | NO | | NULL | |
| user_password | varchar(45) | NO | | NULL | |
| user_type | varchar(30) | NO | | NULL | |
| user_username | varchar(45) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
表comment
:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| comment_id | int(11) | NO | PRI | NULL | auto_increment |
| comment_content | varchar(600) | NO | | NULL | |
| comment_date | datetime | NO | | NULL | |
| comment_title | varchar(300) | NO | | NULL | |
| user_id | int(11) | NO | MUL | NULL | |
| post_id | int(11) | NO | MUL | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
我需要一个返回post_title
,post_summary
,post_date
,number_of_comments_for_this_post
,user_name
的查询,按number_of_comments_for_this_post
排序(降序排列) )。
基本上我需要来自所有三个表的数据。我知道如何离开连接两个表并获得一些数据。例如:
SELECT p.post_title, p.post_summary, p.post_date, u.user_name
FROM post p LEFT JOIN user u ON p.user_id=u.user_id;
如何通过加入第三个表格(评论)来扩展此查询,并添加一个字段 - 首先由大多数评论帖子(desc)排序的“帖子评论数量”?
答案 0 :(得分:2)
加入只是relational algebra运营商。关系代数只是另一种查询形式主义。表是一种关系,对它们的操作产生其他关系。您可以使用多个操作来生成结果关系。 MySQL JOIN
手册页也提示它。
要计算一组行的某些统计信息,您需要aggregate functions。在这种情况下,您需要COUNT
。
SELECT
post_title,
post_summary,
post_date,
COUNT(comment_id) AS comment_cnt,
user.user_name
FROM
post
LEFT JOIN user USING(user_id)
LEFT JOIN comment USING(post_id)
GROUP BY
post_id
ORDER BY
comment_cnt DESC
答案 1 :(得分:1)
像这样的东西
SELECT
p.post_id,
p.post_date,
p.post_summary,
p.post_title,
u.user_name,
COUNT(c.comment_id) AS number_of_post_comments
FROM
post p
LEFT JOIN COMMENT c
ON p.post_id = c.post_id
LEFT JOIN USER u
ON p.user_id = u.user_id
GROUP BY p.post_id
ORDER BY number_of_post_comments DESC
答案 2 :(得分:-1)
简单,只需继续使用正常语法
select p.post_title, p.post_summary, p.post_date, u.user_name
from post p
left join user u
on p.user_id = u.user_id
left join comment c
on p.post_id = c.post_id
除了一个有用的小帮手:
如果您有两个要连接的表中的列具有相同名称的情况,请使用USING() 让你的mysql jedi生活更轻松!
select p.post_title, p.post_summary, p.post_date, u.user_name
from post p
left join user u
USING(user_id)
left join comment c
USING(post_id)