[英]Joining 3 tables with mySQL with 1 query
This has been asked and answered in different instances but all that I've seen doesn't quite work for my problem. 在不同的情况下已经有人问过并回答了这个问题,但是我所看到的一切并不能解决我的问题。
PROBLEM: I have 3 tables I need to pull data from at the same time to compare and retrieve information from. 问题:我需要3个表,同时需要从中提取数据以进行比较和检索信息。 All 3 tables contain an 'email' column.
所有3个表均包含“电子邮件”列。 Now, a user's email from table1 may match same user's email in both table2 AND table3, depending on user's status.
现在,根据用户的状态,来自表1的用户电子邮件可以与表2和表3中的同一用户的电子邮件匹配。 OR an email from table1 will ONLY match either an email in table2 or table3, depending on user's status again.
或者来自table1的电子邮件将仅与table2或table3中的电子邮件匹配,这再次取决于用户的状态。 For example, a user may have a red status (user will show in table2), a blue status (user will show in table3), or both, red and blue (user will show in both, table2 and table3).
例如,用户可能具有红色状态(用户将在表2中显示),蓝色状态(用户将在表3中显示)或红色和蓝色(用户将在表2和表3中同时显示)。
WHAT IS NEEDED: an email from table1 needs to be compared to email in table2 and table3 and return a region value for a given user, which is recorded in table2 and table3 but not in table1. 需要:来自表1的电子邮件需要与表2和表3中的电子邮件进行比较,并返回给定用户的区域值,该值记录在表2和表3中,但不在表1中。 I know.
我知道。 Delightful data architecture!
令人愉快的数据架构! Either way, I was able to
JOIN
table1 to table2 very successfully but I am not sure how to slap on a JOIN
with table3. 无论哪种方式,我都能够非常成功地将table1
JOIN
到table2,但是我不确定如何用table3 JOIN
。
Here's the query for 2 tables: 这是对2个表的查询:
SELECT * FROM table1
INNER JOIN table2
ON table2.email = table1.email
WHERE month = 'numberHere' ORDER BY submitdate DESC
When I simply add another INNER JOIN
my code doesn't break per say but it doesn't give me any rows to be displayed either. 当我简单地添加另一个
INNER JOIN
我的代码不会说每句话,但它也不会给我显示任何行。 So the code below doesn't work despite the many working examples from the web: 因此,尽管网络上有许多可用的示例,但下面的代码无法使用:
SELECT * FROM table1
INNER JOIN table2
ON table2.email = table1.email
INNER JOIN table3
ON table3.email = table2.email
WHERE month = 'numberHere' ORDER BY submitdate DESC
You need to use LEFT JOIN
, so that the joins will succeed even if there's no matching row in one of the tables. 您需要使用
LEFT JOIN
,以便即使表之一中没有匹配的行,连接也将成功。
SELECT * FROM table1
LEFT JOIN table2
ON table2.email = table1.email
LEFT JOIN table3
ON table3.email = table2.email
WHERE month = 'numberHere'
AND (table2.email IS NOT NULL OR table3.email IS NOT NULL)
ORDER BY submitdate DESC
The additional conditions filter out rows that have no match in either of the tables. 其他条件过滤掉两个表中都不匹配的行。
To match the row from table1
in any case and the rows of other tables only if they are, you have to use LEFT JOIN
, joining table2
and table3
with table1
(not table2
with table1
and table3
with tabel2
): 为了匹配从该行
table1
在任何情况下,只有当它们是其他表中的行,你必须使用LEFT JOIN
,加入table2
和table3
与table1
(不table2
与table1
和table3
与tabel2
):
SELECT * FROM table1
LEFT JOIN table2
ON table2.email = table1.email
LEFT JOIN table3
ON table3.email = table1.email
In this way you select values from table1
even without a match in other tables. 这样,即使其他表中没有匹配项,您也可以从
table1
选择值。
See more about SQL Joins: Different SQL Joins 查看有关SQL连接的更多信息: 不同的SQL连接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.