Invalid query count

I have a table whose structure is as follows:

id int userid int status enum ('pending','approved') dop datetime 

Data:

 id userid status dop 1 24 pending 2011-02-14 06:41:32 2 24 pending 2011-02-15 23:02:45 

When I run the following query:

 SELECT count( userid ) FROM t1 WHERE STATUS = 'pending' GROUP BY userid 

This gives me an account like β€œ2,” which is wrong, can anyone tell me what is wrong here? and how to get a real account like 1

+1
source share
6 answers

The group by statement is executed after the count. Use this instead:

 SELECT count( DISTINCT userid ) FROM t1 WHERE STATUS = 'pending' 
+4
source

Do you want to count the number of users with pending status?

 SELECT count(userid) FROM t1 WHERE STATUS = 'pending' GROUP BY status, userid 
+1
source

Try adding the user id in the select clause:

 SELECT userid, count( userid ) FROM t1 WHERE STATUS = 'pending' GROUP BY userid 
0
source

Maybe adding DISTINCT () to userid?

0
source
 SELECT count( DISTINCT userid ) FROM t1 WHERE STATUS = 'pending' GROUP BY userid 
0
source

You can use the COUNT (DISTINCT ()) construct, it allows you to read different non-NULL values ​​( document )

Try the following sentence:

 SELECT count( DISTINCT( userid ) ) FROM t1 WHERE STATUS = 'pending' GROUP BY userid 

NTN!

0
source

Source: https://habr.com/ru/post/970657/


All Articles