gpt4 book ai didi

mysql - GROUP_CONCAT 一条选择语句

转载 作者:可可西里 更新时间:2023-11-01 07:53:35 24 4
gpt4 key购买 nike

这是我的 SQL

SELECT c.name AS 'name',
COUNT(sc.condition_id) AS 'conditions_count'
FROM reviews r, strain_conditions sc
LEFT JOIN conditions c ON sc.condition_id = c.id
WHERE sc.strain_id = 1 && sc.review_id = r.id && r.moderated = 1
GROUP BY sc.condition_id
ORDER BY conditions_count DESC
LIMIT 3;

哪个返回

+---------------+------------------+
| name | conditions_count |
+---------------+------------------+
| Fiber Myalgia | 2 |
| Anxiety | 2 |
| ADHD | 1 |
+---------------+------------------+

我需要做的是 GROUP_CONCAT 名称结果,所以我的最终结果很简单

“纤维肌痛、焦虑、多动症”

最佳答案

将其包装在子查询中。试试这个,

SELECT GROUP_CONCAT(f.name) as `name`
FROM
(
SELECT c.name AS 'name',
COUNT(sc.condition_id) AS 'conditions_count'
FROM reviews r, strain_conditions sc
LEFT JOIN conditions c ON sc.condition_id = c.id
WHERE sc.strain_id = 1 && sc.review_id = r.id && r.moderated = 1
GROUP BY sc.condition_id
ORDER BY conditions_count DESC
LIMIT 3
) f

关于mysql - GROUP_CONCAT 一条选择语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101298/

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