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个条目时,即在那个“群体”中。我该怎么做?
答案 0 :(得分:3)
按聚合过滤是有效的,但需要进入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`)
代替。