Zend_Db_Select - 加入和计数 - 可能的Zend Bug?

时间:2010-02-06 05:50:31

标签: mysql sql zend-framework mysql-error-1140

我在使用Zend_Db_Table_Select从SQL查询中获取COUNT()时遇到问题,我认为这可能是一个可能的错误,因为它应该生成的SQL实际上有效。这是Zend Select查询:(这是一个Zend_Db_Table,在这个例子中重命名为table1

    $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);

这给出了错误:

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`

然而,这个查询...

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]

...在针对数据库运行时返回预期结果而没有错误。任何想法是什么,为什么错误,以及如何解决它?

1 个答案:

答案 0 :(得分:2)

您是否尝试过查看zend_db生成的实际查询?