zend left join count不工作

时间:2014-09-23 12:24:37

标签: php mysql zend-framework join count

我在多连接计数时遇到问题

$query = $this->select()
        ->setIntegrityCheck(false)
        ->from(array('u' => 'users'))
        ->join(array('c' => 'clients'), 'u.id = c.user_id', 'COUNT(c.user_id) as clientsCount')
        ->join(array('emails' => 'u_emails'), 'u.id = emails.user_id', 'COUNT(emails.user_id) as emailsCount')
        ->join(array('sms' => 'u_sms'), 'u.id = sms.user_id', 'COUNT(sms.user_id) as smsCount')
        ->where('u.id=?', (int) USER_ID)->group('u.id');

1 个答案:

答案 0 :(得分:1)

我认为这可以帮助您摆脱这个问题:

$this->select()
     ->from(array('u' => 'users'), array(
            'clientsCount' => new Zend_Db_Expr("(SELECT COUNT(*) FROM clients WHERE clients.user_id = u.id)"),
            'emailsCount' => new Zend_Db_Expr("(SELECT COUNT(*) FROM emails WHERE emails.user_id = u.id)"),
            'smsCount' => new Zend_Db_Expr("(SELECT COUNT(*) FROM sms WHERE sms.user_id = u.id)"),
     )
     ->where('u.id=?', (int) USER_ID)
     ->group('u.id');

我无法测试查询,所以请随时询问您是否有任何问题。

编辑我的回答,请再试一次