Thursday, 30 August 2018

Mysql select account on the left join with the condition does not work


I need to count number of records on left table, i read other questions and end up with this query but the condition on COUNT is ignored


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

is MySQL 5 DB

you cant specify a condition inside COUNT statment.
try this
   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

0 comments:

Post a Comment