简体   繁体   English

如何在MySQL中加入这5张表?

[英]How to join this 5 tables in mysql?

I have 5 tables that I need to connect to get the necessary data. 我需要连接5个表才能获取必要的数据。

Table1 表格1

id | number
1  | 1
2  | 5

Table 2 表2

id | number | user_id
1  | 1      | 9
2  | 5      | 8

Table 3 表3

id | name | 
8  | john | 
9  | jane | 

Table 4 表4

id | email
6  | johndoe@example.com

Table 5 表5

id | table4_id | table3_id
1  | 6         | 8

Table 1 is my main table and I want to add the name and email from table 3 and 4 respectively to my select query of table 1, but in order to do so, I would need to use table 2 and 5 to connect them as there is no direct relationship between table 1 and table 3 and 4. I only know how to join 3 tables and not 5 tables and it seems confusing to me on how to proceed with this. 表1是我的主表,我想将表3和表4的名称和电子邮件分别添加到表1的选择查询中,但是为此,我需要使用表2和表5进行连接表1与表3和4之间没有直接关系。我只知道如何联接3个表,而不联接5个表,这似乎使我感到困惑。

I followed the link here to join Table 1,2 and 3. But I don't know how to proceed with table 4 and 5. 我点击了此处的链接以连接表1,2和3。但是我不知道如何进行表4和表5。

This is the query I tried: 这是我尝试过的查询:

SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name,
table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2
    INNER JOIN table3
        //this query will work if I don't include this 2 inner joins
         INNER JOIN table4
            INNER JOIN table5
            ON table3.id = table5.table3_id
         ON table5.table4_id = table4.id
       //
      ON table2.user_id= table3.id
ON table2.number = table1.number;

ERROR: (if included the inner join for table 4 and 5) 错误:(如果包括表4和5的内部联接)

Error Code: 1064. You have an error in your SQL syntax; 错误代码:1064。 check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON table2.user_id= table3.id 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'ON table2.user_id = table3.id附近使用

The right Syntax is select .. from .. join .. on .. join ..on .... select .. from .. join .. on .. join ..on ....右边的语法select .. from .. join .. on .. join ..on ....

SELECT table1.number, table2.number, table2.user_id, table3.id, table3.name,  table4.id, table4.email, table5.table4_id, table3_id
FROM table1
LEFT JOIN table2 ON table2.number = table1.number
INNER JOIN table3 ON table2.user_id= table3.id
INNER JOIN table5 ON table3.id = table5.table3_id
INNER JOIN table4 ON table5.table4_id = table4.id

Try with the below structure. 请尝试以下结构。 I made it from the structure you given in question 我是根据您所提出的结构制作的

select t1.number, t2.number, t2.user_id, t3.id, t3.name,t4.id, t4.email, t5.t4_id, t3_id 
from table5 as t5 
join table4 as t4 on t5.table4_id = t4.id 
join table3 as t3 on t5.table3_id=t3.id 
join table2 as t2 on t2.user_id = t3.id 
join table1 as t1 on t2.number=t1.number

Try Below code. 尝试下面的代码。 Hope this will work. 希望这会起作用。

Select t3.name,t4.email,t1.number,t2.user_id 
from table3 t3 JOIN table5 t5 ON t3.id=t5.table3_id 
JOIN table4 t4 ON t4.id=t5.table4_id
JOIN table2 t2 ON t2.user_id=t3.id
JOIN table1 t1 ON t1.number=t2.number;

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

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