我在使用 Zend_Db_Table_Select 从 SQL 查询中获取 COUNT() 时遇到问题,我认为这可能是一个错误,因为它应该生成的 SQL 确实有效。这是 Zend Select Query:($this 是 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]
...在对数据库运行时返回没有错误的预期结果。任何想法发生了什么,为什么会出现错误,以及如何解决它?