views:

315

answers:

1

I'm having an issue getting a COUNT() from a SQL query using Zend_Db_Table_Select, and I think it may be a possible bug because the SQL it should be generating actually works. Here's the Zend Select Query: ($this is a Zend_Db_Table, renamed to table1 in this example)

    $select = $this->select();
    $select->setIntegrityCheck(false);

    // Select Count
    $select->from($this, array("COUNT(*) as 'COUNT'"))
           ->joinLeft('users', 'table1.userID = users.userID')
           ->joinLeft('table2', 'users.anotherKey = table2.anotherKey');

    // Add Where clause after join
    $select->where('users.anotherKey = ?', $anotherKeyValue);

This gives the error:

SQLSTATE[42000]: Syntax error or access violation: 1140 
Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is 
illegal if there is no GROUP BY clause`

However, this query...

SELECT COUNT(*) AS 'count' FROM table1
    LEFT JOIN users ON table1.userID = users.userID
    LEFT JOIN table2 ON users.anotherKey = table2.anotherKey
    WHERE users.anotherKey = [anotherKeyValue]

...returns the expected results with no errors when run against the database. Any ideas whats going on, why the error, and how to get around it?

+1  A: 

have you tried to see actual query, that zend_db produce?

zerkms
Ha, well that was a good idea. I found out the issue was that I thought the default 3rd param to joinLeft was to select nothing. In fact, it selects tableName.* instead. This turned the query into `SELECT COUNT(*) AS 'COUNT', users.*, table2.* FROM table1 ....` which caused the issue. I passed it null instead of nothing and now the query is as I thought it was. Thanks again.
Ryan