简体   繁体   English

MySQL加入多个表

[英]MySQL Joining Multiple Tables

I have three tables: 我有三张桌子:

t1.estimate, t1.mid, t1.description, t1.status

t2.mid, t2.mname, t2.mphone, t2.memail

t3.estimate, t3.action

I need to join these tables, but that issue that I am having is that t2 and t3 may contain no records to join to t1. 我需要加入这些表,但我遇到的问题是t2和t3可能不包含连接到t1的记录。 Table t1 is the primary table that will have the filter applied. 表t1是将应用过滤器的主表。 Table t2 will 99.9% of the time contain a match when join 'mid'. 当加入'mid'时,表t2将99.9%的时间包含匹配。 But table t3 is a table that only stores information and creates an estimate when a user enters it into the table. 但是表t3是一个只存储信息并在用户将其输入表格时创建估计的表格。 There can be 40,000 plus records in t1, but only 5,000 in t3. t1中可以有40,000多条记录,但t3中只有5,000条记录。

Here is my current code, but it's only displaying records that are in all three tables. 这是我当前的代码,但它只显示所有三个表中的记录。 I would like values to be shown from t1 even if there are no records to join on t2 and t3. 我希望从t1显示值,即使在t2和t3上没有要加入的记录。

SELECT DISTINCT
    t1.estimate, t1.mid, t2.mname, t1.description,
    t1.status, GROUP_CONCAT(t3.action)
FROM t1
LEFT OUTER JOIN t2 ON t1.mid = t2.mid
LEFT OUTER JOIN t3 ON t1.estimate = t3.estimate
WHERE t1.status LIKE '0%'
GROUP BY t3.estimate

您需要更改GROUP BY以使用t1.estimate而不是t3.estimate因为如果没有连接记录, t3.estimate将为NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM