gpt4 book ai didi

mysql - 计算 GROUPed BY 的行数

转载 作者:行者123 更新时间:2023-11-29 02:04:33 26 4
gpt4 key购买 nike

SELECT id,name,SUM(`lsa`) AS lst FROM 
table_a AS sn,
table_b AS fb
WHERE sn.uid=fb.uid
AND COUNT(`lsa`)=6
GROUP BY sn.uid
ORDER BY SUM(`lsa`)

我有一个这样的查询,显然 AND COUNT(lsa)=6 位不是有效语法。我迷路了。

我想要实现的是一个列表,按 lsa 的总和排序,按 uid 分组,仅当有该 uid 的 6 个条目,即在该“组”中。我应该怎么做?

最佳答案

按聚合过滤是有效的,但需要进入 HAVING 子句,而不是 WHERE 所以使用

SELECT id,
name,
SUM(`lsa`) AS lst
FROM table_a AS sn
JOIN table_b AS fb
ON sn.uid = fb.uid /*Using Explicit Join syntax*/
GROUP BY sn.uid, /*And avoiding MySQL GROUP BY extension*/
id,
name
HAVING COUNT(`lsa`) = 6
ORDER BY SUM(`lsa`)

相反。

关于mysql - 计算 GROUPed BY 的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8914843/

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