Search code examples
mysqldoctrine-ormdql

COUNT query on a boolean column with DQL


I have a "simple" question whose answer seems not to be in the official DQL docs. Given an application built with Doctrine2, suppose to have a boolean column (let say B for entity Foo), how to count the number of rows such that the value in B is true (or false)?

PS: If may helps, I use MySql


Solution

  • If you wish to count the number of "true" values:

        'SELECT COUNT(f.B) as numtrue  
                FROM Foo f 
                WHERE f.B = TRUE'
    

    To get the number of occurence of "false" values:

        'SELECT COUNT(f.B) as numfalse  
                FROM Foo f 
                WHERE f.B = FALSE'