【发布时间】:2014-09-23 12:24:36
【问题描述】:
ia 在多连接中的计数有问题
$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');
【问题讨论】:
-
我认为您的问题是所有计数都返回相同的值?
-
是的,我在 u_emails 中有 4 行,在 u_sms 中有 2 行,在客户端中有 2 行。并且所有计数都返回 16。我不知道问题出在哪里。它看起来像倍增。
-
因为你使用了
Group。在COUNT组之后只计算返回的行数(这是 16,因为有 16 个唯一用户 ID。我认为您可以轻松地用一些子查询替换此查询。 -
如何使用子查询进行相同的查询?感谢您的帮助
标签: php mysql zend-framework join count