gpt4 book ai didi

php - 如何在 Zend_Db_Select 中进行 COUNT AS 操作?

转载 作者:行者123 更新时间:2023-11-29 13:39:04 25 4
gpt4 key购买 nike

所以我有这个查询:

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)'))

同样的错误。

最佳答案

这应该可以正常工作:

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

关于php - 如何在 Zend_Db_Select 中进行 COUNT AS 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18361406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com