0

我有以下查询:我刚刚注意到当他们有 0 时没有汇总用户的计数item_count。我曾尝试调整此查询,因为我之前犯过此错误,但不是这种更复杂的查询,我需要如何调整它才能显示用户的 item_count 即使为 0?

SELECT u.ID AS user_id,
                u.date_expired AS user_date_expired,
                u.first_name AS first_name,
                u.last_name AS last_name,
                COUNT(i.id)    AS item_count
FROM `user` `u`
LEFT JOIN `item` `i` ON i.recruiter_id = u.id
WHERE ((`u`.`id` IN ('15', '2', 13)) AND (i.date_added >
DATE_SUB(u.date_expired, INTERVAL  30 DAY)))
GROUP BY `u`.`id`

谢谢

强尼

4

1 回答 1

1

试试这个方法

SELECT u.id           AS user_id,
       u.date_expired AS user_date_expired,
       u.first_name   AS first_name,
       u.last_name    AS last_name,
       COUNT(*)       AS item_count
  FROM user u LEFT JOIN item i 
    ON i.recruiter_id = u.id
   AND i.date_added > u.date_expired - INTERVAL  30 DAY
 WHERE u.id IN (15, 2, 13) 
 GROUP BY u.id, u.date_expired, u.first_name, u.last_name
于 2013-09-26T05:22:52.367 回答