gpt4 book ai didi

php - SQL查询从3个表中选择多对多

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

我有 3 张 table :

帖子:

   post_id    |    title---------------------------------      1       |   post number 1      2       |   post number 2      3       |   post number 3

类别:

   category_id   |   cate_name--------------------------------        1        |     video        2        |     review        3        |     gameplay

post_categories:

 post_id   |   category_id---------------------------------   1       |      1   2       |      2   3       |      3   2       |      1   3       |      2   1       |      3

如何选择帖子标题类别名称?结果应如下所示:

        title       |   cate_name-----------------------------------------   post number 1    |   video, gameplay    post number 2    |   video, review   post number 3    |   review, gameplay

这可能吗?

最佳答案

好的,试试这个。

SELECT DISTINCT
post.title as title,
GROUP_CONCAT(categories.cate_name) as cate_name
FROM post
LEFT JOIN post_categories
ON post_categories.post_id = post.post_id
LEFT JOIN categories
ON post_categories.category_id = categories.category_id
GROUP BY post.post_id;

关于php - SQL查询从3个表中选择多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39136060/

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