我在使用 Zend_Db_Table_Select 从 SQL 查询获取 COUNT() 时遇到问题,我认为这可能是一个错误,因为它应该生成的 SQL 实际上有效。这是 Zend Select Query:($这是一个 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 生成的实际查询?
关于mysql - Zend_Db_Select - 连接和计数 - 可能的 Zend Bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212215/