简体   繁体   English

MySQL - 如何从表中获取结果,以及在一个查询中有多少个连接项的计数?

[英]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.

相关问题 如何编写一个MySQL查询,该查询将限制一个或多个联接表的结果,并计算一个或多个联接表中的项目数? - How to write a MySQL query that will limit the results of a joined table or tables and also count the number of items in the joined table or tables? MySQL查询获取联接表结果 - MySQL query to get joined table results 如何按联接表的行(而非列)中的值对mysql查询结果排序? - How do I order mysql query results by values from a joined table's rows (not columns)? 如何显示一个表在不同列中的mysql计数结果? - How to display mysql count results from one table in different columns? 如何创建MYSQL查询以获取特定结果(从一栏中计算所有唯一值) - How to create MYSQL query to get specific results (count all unique values from one column) MYSQL-如何平均内部联接表结果返回的票数 - MYSQL - how to average returned votes from inner joined table results MySQL-联接表中没有多少人? - MySQL - How many people are NOT in a joined table? 无法计算联接表mySQL的结果 - Can't Count results from Joined Table mySQL 如何通过MySQL查询从联接表中检索最低价格 - How to retrieve min price via mysql query from a joined table MySQL - 如何使用左连接从on子句中的连接表中获取一行? - MySQL - How can I use left join to get exactly one row from a joined table in the on clause?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM