我在使用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]
…在针对数据库运行时返回预期结果而没有错误.任何想法是什么,为什么错误,以及如何解决它?
最佳答案 你有没有试过看到zend_db生成的实际查询?