我有这个查询会计算ID(' id_avocat')出现的次数。我想选择id只出现1次的id_avocat;
这是我的疑问:
SELECT id_avocat a, COUNT(*) asd ,`planificare` FROM `generare_liste` WHERE `planificare`="instantelor" AND asd <2 GROUP BY a ORDER BY COUNT(*) DESC
我收到此错误:Unknown column 'asd' in 'where clause'
,但如果我使用&#39; asd&#39;只是在选择中,而不是在where子句中,我没有收到任何错误。
如何只选择ID Where count(*) is smaller that 2
?
答案 0 :(得分:0)
使用HAVING
子句:
SELECT id_avocat, COUNT(*) asd ,`planificare`
FROM `generare_liste`
WHERE `planificare`="instantelor"
GROUP BY a
HAVING COUNT(*) = 1
BTW - a
中的GROUP BY
列是什么?
答案 1 :(得分:0)
从你的问题中假设你的表结构..
ID_AVOCAT ASD PLANIFICARE A
1 3 instantelor a
2 0 instantelor c
3 1 instantelor b
4 3 instantelor b
6 2 instantelor b
然后你的查询必须像..
SELECT id_avocat,
asd ,
`planificare`
from `GENERARE_LISTE`
group by a
having count(*) < 3
<强> DEMO 强>