我有一个无法用Zend_Db_Select构建的查询
SELECT `f`.*,
(SELECT Sum(x) AS `y`
FROM z AS pf
WHERE pf.q_id = f.id) AS w
FROM f ...
WHERE ...
GROUP BY `f`.`id`
所以目前我正在手动运行$db->fetchAll($sql)
。
我如何获得
select f.* , (select ...) as `something` from ...
我在考虑使用->column('f.*, (select...)')
,但它不起作用,
感谢
答案 0 :(得分:0)
我会推荐JOIN。您可能会获得更好的性能,因为子选择通常很难进行数据库优化。用Zend_Db_Select写这个也很容易。或者,新的Zend_Db_Expr
可能适用于此。
$select = $db->select()
->from('f', array('f.foo', 'f.bar', new Zend_Db_Expr('SELECT Sum(x) AS `y`
FROM z AS pf
WHERE pf.q_id = f.id') => 'f'))
->where(...);