繁体   English   中英

选择不同数据的计数

[英]Select the count of distinct data

我有此查询,将计算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' ,但是如果仅在select中而不是在where子句中使用“ asd”,则不会出现任何错误。

如何仅选择id Where count(*) is smaller that 2的ID?

使用HAVING子句:

SELECT id_avocat, COUNT(*) asd ,`planificare` 
FROM `generare_liste` 
WHERE `planificare`="instantelor" 
GROUP BY a 
HAVING COUNT(*) = 1

顺便说一句-您的GROUP BY aa什么?

让我们根据您的问题假设您的表格结构。

    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

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM