gpt4 book ai didi

mysql - 如何为此编写选择查询

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

我有两张这样的 table

job(id,title,location,...)

job_page(id,id_job,id_page,page_name)

例如,我想编写一个选择查询来填充以下列的表格:

+_______+________+__________+_______+
|Job Id | Titlte | Location | Page |
+_______+________+__________+_______+
|1 | Hot job| Somewhere| Page1 |
| | | | Page2 |
+_______+________+__________+_______+

如何为此编写选择查询?

我尝试过,但结果并不像我预期的那样:

SELECT jp.page_name,j.* FROM jobs j
LEFT JOIN job_pages jp ON jp.id_job = j.id
GROUP BY j.id;

最佳答案

您需要使用group_concat:

 SELECT j.id, j.title, j.location, group_concat(jp.page_name)
FROM jobs j LEFT JOIN job_pages jp ON jp.id_job = j.id
GROUP BY j.id, j.title, j.location;

在 MySQL 中,您可以缩写 GROUP BY:

 SELECT j.id, j.title, j.location, group_concat(jp.page_name)
FROM jobs j LEFT JOIN job_pages jp ON jp.id_job = j.id
GROUP BY j.id;

但这确实让像我这样的多数据库用户感到困扰。

关于mysql - 如何为此编写选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7990072/

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