Mysql选择左连接计数,条件不起作用

时间:2013-11-17 11:32:00

标签: mysql sql select count

我需要计算左表上的记录数,我读了其他问题并最终得到了这个查询,但忽略了COUNT上的条件

SELECT  a.name, COUNT( f.status <> 'e' ) AS total               
FROM    album AS a LEFT JOIN photo AS f
        ON a.id = f.idalbum
WHERE   a.iduser = 4
GROUP   BY a.id

是MySQL 5 DB

2 个答案:

答案 0 :(得分:3)

您无法在COUNT语句中指定条件。

试试这个

   SELECT  a.name, COUNT( f.status  ) AS total               
   FROM    album AS a LEFT JOIN photo AS f
    ON a.id = f.idalbum
   WHERE   a.iduser = 4 or f.status <> 'e'
   GROUP   BY a.id

答案 1 :(得分:1)

如果你这样做:

SELECT  a.name, COUNT( f.status ) AS total               
FROM    album AS a LEFT JOIN photo AS f
    ON a.id = f.idalbum
WHERE   a.iduser = 4 and f.status != 'e'
GROUP   BY a.id