我正在尝试将以下sql查询构建为Zend_Db_Select对象
$sql = "
SELECT
u.id,
u.email,
s.nic as nic,
(SELECT COUNT(*) FROM event WHERE user_id = u.id AND event='login') as logins,
(SELECT COUNT(*) FROM event WHERE user_id = u.id AND event='export') as exports,
(SELECT MAX(time) FROM event WHERE user_id = u.id AND event='login') as lastlogin,
(DATEDIFF(u.expire_date, NOW())) as daysleft
FROM
user u,
seller s
WHERE
u.seller_id = s.id";
没有运气。我无法在Zend_Db_Select对象中使用subquerys。
是否可以通过连接而不是subquerys来实现相同的结果?
任何关于如何使这项工作的提示都将受到高度赞赏。
答案 0 :(得分:0)
尝试这样的事情:
$select->from(array('u'=>'user'),array('id','email'));
$select->join(array('s'=>'seller'),'s.id = u.seller_id', array('nic'));
$select->columns(array(
'logins' =>"(SELECT COUNT(*) FROM event WHERE user_id = u.id AND event='login')",
'exports' =>"(SELECT COUNT(*) FROM event WHERE user_id = u.id AND event='export')",
'lastLogin' =>"(SELECT MAX(time) FROM event WHERE user_id = u.id AND event='login')",
'daysLeft' =>"(DATEDIFF(u.expire_date, NOW()))",));
$stmt = $select->query();
var_dump($stmt->fetchAll());