gpt4 book ai didi

mysql - SQL 查询不会为连接中的每个匹配项返回多行

转载 作者:行者123 更新时间:2023-11-29 09:02:59 25 4
gpt4 key购买 nike

我有 2 张 table :

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 | |
+---------------------+---------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

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 | |
+-----------------------+----------------+------+-----+---------+----------------+

我想要做的是显示solution_sections表中的section_name列表,以及建议_solution_comments表中每个section_name的n个匹配项。因此,对于每个部分名称,查询应该获取与其关联的 suggest_solution_comments 列表。

这些表通过 suggest_solution_comments.solution_part 和 Solution_sections.solution_section_id 链接

这是我到目前为止正在尝试的:

select section_name , comment , solution_part , display_order from solution_sections 
left join suggested_solution_comments on
solution_sections.solution_section_id = suggested_solution_comments.solution_part
where suggested_solution_id = 188 OR suggested_solution_id IS NULL
group by display_order;

问题是查询获取了section_name 的列表,以及每个section_name 一个匹配的注释,但不超过一个注释。知道为什么它没有得到所有相关的评论吗?

谢谢!!

最佳答案

GROUP BY 可能会解释您在没有它的情况下所看到的尝试(根据评论进行更新)

select section_name , comment , solution_part , display_order 
from solution_sections
left join suggested_solution_comments on
solution_sections.solution_section_id = suggested_solution_comments.solution_part
where suggested_solution_id = 188 OR suggested_solution_id IS NULL
order by display_order;

一些评论:

您的数据模型似乎有点奇怪...例如 solution_partint(3) 并链接到 solution_section_id 这是转是一个autoincrement int(10)

关于mysql - SQL 查询不会为连接中的每个匹配项返回多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com