Mysql连接查询
我在数据库中使用了三个表,我需要的查询应该组合所有三个表。 这些表如下所示: 表A:Mysql连接查询,mysql,Mysql,我在数据库中使用了三个表,我需要的查询应该组合所有三个表。 这些表如下所示: 表A: id | name | status --------------------------------- 5 | john | 1 7 | mike | 1 8 | jane | 0 表B: send-id | receive-id | notification --
id | name | status
---------------------------------
5 | john | 1
7 | mike | 1
8 | jane | 0
表B:
send-id | receive-id | notification
-------------------------------------------
12 | 5 | 1
5 | 23 | 1
8 | 14 | 1
19 | 7 | 2
14 | 5 | 1
表C:
registered-id | status-reg
----------------------------------
5 | 7
7 | 7
8 | 7
9 | 3
我需要列出具有表A中的状态1和表C中的状态7的用户,并且这些用户没有在表B列的“接收id”中列出,在“通知”列中的值为2
本例中的结果将是:
id | name | status | notification
--------------------------------------------------
5 | john | 1 |
ID号为7和8的用户将被排除在列表之外。ID为7的用户(因为它位于字段的表B中)在表通知中接收值为2的ID,而ID为8的用户(因为它在表a中的状态为0)
我如何在一个查询中做到这一点
感谢您的帮助。让我们尝试未经测试的,可能包含错误:
SELECT a.id, a.name, a.status, b.notification
FROM a
JOIN c ON (a.id = c.registered-id)
JOIN b ON (a.id = b.receive-id)
WHERE a.status = 1
AND c.status-reg = 7
AND b.notification <> 2
希望这能让你走上正轨
Select ID, Name, Status, Notification
FROM TableA A
LEFT JOIN TableB B on B.receive-id = a.ID
and b.notification <> 2
INNER JOIN tableC C on A.ID = C.Registered-id
WHERE a.status=1
and c.status-reg=7
我认为您需要所有用户,即使他们在表B中没有记录,只要状态为1和7。因此,我认为需要一个左连接,并且必须对连接施加限制,除非您想在where子句中处理null