[英]MySQL - how to get results back from a table, and the count of how many joined items there are in one query?
I have a query like this: 我有这样的查询:
select display_order , section_name , solution_section_id from solution_sections order by display_order
It is very basic and gets the sections of a particular discussion. 这是非常基础的,并得到特定讨论的部分。 It works.
有用。
What I want to do is to also display the number of comments in each of the section. 我想要做的是也显示每个部分的评论数量。 So I want to do a join on the comments table and do a count on how many comments there are.
所以我想在评论表上进行联接,并计算有多少评论。
Here is the schema for the other tables: 以下是其他表的架构:
mysql> describe suggested_solution_comments;
+-----------------------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+----------------+------+-----+---------+----------------+
| comment_id | int(10) | NO | PRI | NULL | auto_increment |
| problem_id | int(10) | NO | | NULL | |
| suggested_solution_id | int(10) | NO | | NULL | |
| commenter_id | int(10) | NO | | NULL | |
| comment | varchar(10000) | YES | | NULL | |
| solution_part | int(3) | NO | | NULL | |
| date | date | NO | | NULL | |
| guid | varchar(50) | YES | UNI | NULL | |
+-----------------------+----------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> describe solution_sections;
+---------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------+------+-----+---------+----------------+
| solution_section_id | int(10) | NO | PRI | NULL | auto_increment |
| display_order | int(10) | NO | | NULL | |
| section_name | varchar(1000) | YES | | NULL | |
+---------------------+---------------+------+-----+---------+----------------+
So it would have to be a join on solution_section_id and solution_part (those are the foreign keys even though they are named somewhat inconsistently) where problem_id = some id. 所以它必须是一个关于solution_section_id和solution_part的连接(即使它们被命名有些不一致,这些是外键),其中problem_id = some id。
But how would I get the count of the number of returned comments in the suggested_solution_comments table? 但是,如何获取suggested_solution_comments表中返回的注释数量?
Thanks! 谢谢!
SELECT solution_sections.display_order, solution_sections.section_name, solution_sections.solution_section_id, COUNT(suggested_solution_comments.comment_id) FROM solution_sections, suggested_solution_comments GROUP BY solution_sections.solution_section_id
Maybe try something like this? 也许尝试这样的事情? Its been awhile since i touched table joins, and your table naming looks pretty confusing to me.
自从我触及表连接以来,它已经有一段时间了,你的表命名看起来让我很困惑。
UPDATED with outer join: 外连接更新:
select s.display_order, s.section_name, s.solution_section_id
,count(c.comment_id) AS comment_count
from solution_sections s
left outer join suggested_solution_comments c ON (c.solution_part = s.solution_section_id)
group by s.display_order, s.section_name, s.solution_section_id
order by display_order
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.