gpt4 book ai didi

php - 组连接结果不准确

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

我正在使用 GROUP_CONCAT() 来显示多个类别,但我没有得到确切的结果。对于表连接,我使用 IN() 子句,请帮助我。

The query only displays a single category. in database category id store like 2,5,8

$sql = "SELECT u.*, GROUP_CONCAT(c.name SEPARATOR ',') as cname, s.name as sname
FROM course u
INNER JOIN category c ON c.id IN (u.category_id)
INNER JOIN status s ON s.id = u.status_id
WHERE u.is_deleted = 0 AND u.id = $id $app
LIMIT 1";

我想要类别表中的多个类别。

最佳答案

group_concat 是一个聚合函数,当您还需要未聚合的列时,为了正确使用这些函数,需要 GROUP BY 子句和用于聚合级别的列名称

    SELECT  GROUP_CONCAT(c.name SEPARATOR ',') as cname, s.name as sname
FROM course u
INNER JOIN category c ON c.id IN (u.category_id)
INNER JOIN status s ON s.id = u.status_id
WHERE u.is_deleted = 0 AND u.id = $id $app
GROUP BY s.name
ORDER BY s.name
LIMIT 1;

关于php - 组连接结果不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55933487/

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