Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare row count in a MYSQL statement?

Tags:

sql

mysql

Lets say I have this SQL Table

id | name | date
1  | as   | 0000-00-00
1  | df   | 0000-00-00
1  | fg   | 0000-00-00
1  | hj   | 0000-00-00
2  | xc   | 2011-2-32
2  | op   | 2011-3-21
3  | er   | 2011-2-43
4  | po   | 0000-00-00
4  | ui   | 0000-00-00

I want to get the id that has 4 rows of date which has a value of 0000-00-00, which would be 1

The SQL statement Im thinking of is something like this... (This is obviously wrong)

SELECT id
FROM table1
WHERE COUNT(date = 0000-00-00) = 4

I'm open to other answers in case you have a better/efficient solution

like image 931
JohnSmith Avatar asked Dec 03 '25 19:12

JohnSmith


1 Answers

SELECT id, count(*)
FROM table1
WHERE date = 0000-00-00
GROUP BY id
HAVING count(*) = 4
like image 82
StevieG Avatar answered Dec 06 '25 09:12

StevieG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!