【发布时间】:2018-12-28 03:25:01
【问题描述】:
如果用户有记录,我想连接三个表并检查另外两个表。
表格:
table1
id | username | is_active
table2
id | userid | amount
table3
id | userid | amount
我想获取并统计 'is_active' = 1 且表 2 和表 3 上没有记录的用户
我正在尝试这个:
SELECT c.id,c.is_active,
COUNT(c.id) AS count,
COUNT(a.userid) AS count1,
COUNT(b.userid) AS count2
FROM `tbl_members` c
LEFT JOIN `table1` b
ON c.`id` = b.`userid`
LEFT JOIN `table2` a
ON b.`userid` = a.`userid`
WHERE c.`is_active` = 1
GROUP BY c.id
【问题讨论】:
-
您对多个列使用
count聚合,因此您需要为它们提供GROUP BY- 可能与 stackoverflow.com/questions/2421388/… 重复 -
我会检查的
-
这是一个不同的问题
-
我想要的是,获取所有 is_active = 1 且表 2 和表 3 上没有记录的用户
标签: mysql