0

所以我有这个查询:

select u.*, count(mi.media_id) as media_count
from user as u
left join media_items as mi on mi.user_id = u.user_id
where u.is_enabled = 1 and
group by u.user_type
having media_count > 0;

我正在尝试将其转换为 Zend_Db_Select。到目前为止,除了媒体的数量,我什么都有。这是我尝试过的:

$select->from(array('u' => 'user'), array('*', new Zend_Db_Expr('COUNT(mi.media_id) AS media_count'))
       ->joinLeft('mi' => 'media_items'), 'mi.user_id = u.user_id')
       ->where('u.is_enabled = 1')
       ->group('u.user_type')
       ->having('media_count > 0');

这让我得到了错误:

"Mysqli prepare error: Unknown column 'media_count' in 'having clause'"

如何以 Zend 方式创建此语句?

另外,我输出了这个创建的查询并在 MySQLWorkBench 上运行它,它运行得很好。

编辑我刚刚尝试过:

->columns('media_count' => new Zend_Db_Expr('COUNT(mi.media_id)'))

同样的错误。

4

1 回答 1

1

这应该可以正常工作:

$select->from(array('u' => 'user'), array('*', 'media_count' => 'count(mi.media_id)'))
   ->joinLeft('mi' => 'media_items'), 'mi.user_id = u.user_id')
   ->where('u.is_enabled = 1')
   ->group('u.user_type')
   ->having('media_count > 0');
于 2013-08-21T19:09:05.533 回答