如何比较MYSQL语句中的行数?

时间:2011-10-19 12:35:35

标签: mysql sql

假设我有这个SQL表

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

我想获得有4行日期的id,其值为0000-00-00,这将是1

我想到的SQL语句是这样的...... (这显然是错误的)

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

如果你有更好/更有效的解决方案,我会接受其他答案

3 个答案:

答案 0 :(得分:6)

SELECT id, count(*)
FROM table1
WHERE date = 0000-00-00
GROUP BY id
HAVING count(*) = 4

答案 1 :(得分:2)

这应该是你要找的东西

SELECT id, count(id)
FROM table1
WHERE date = 0000-00-00
HAVING count(id) = 4
GROUP BY id

答案 2 :(得分:2)

SELECT id, count(id)
FROM table1
WHERE date = '0000-00-00'
GROUP BY id
HAVING count(id) = 4