简体   繁体   中英

Select the count of distinct data

I have this query that will count how many times id('id_avocat') appear. I want to select the id_avocat where id appears just 1 time;

This is my query :

   SELECT id_avocat a, COUNT(*) asd ,`planificare` FROM `generare_liste` WHERE `planificare`="instantelor" AND  asd <2   GROUP BY a ORDER BY COUNT(*) DESC 

I'm getting this error: Unknown column 'asd' in 'where clause' ,but if I use the 'asd' just in select ,not in where clause,I'm not getting any error.

How can I select just the id Where count(*) is smaller that 2 ?

Use the HAVING clause:

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

BTW -- what is column a in your GROUP BY ?

let assume your table structure from your question..

    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

then your query must be like..

SELECT id_avocat,
   asd ,
  `planificare`
from `GENERARE_LISTE`
group by a
having count(*) < 3

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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