简体   繁体   English

如何在MYSQL中使用INNER / OUTER JOIN

[英]How to use INNER/OUTER JOIN in MYSQL

I have 3 tables which contain different types of data related to each other. 我有3个表,其中包含彼此相关的不同类型的数据。 the tables populate via an excel spreadsheet. 表格通过excel电子表格填充。 I have: 我有:

table1            table2          table3
item_number       item_number     item_number
desc              desc            qty_sold
qty_instock       vdf_cost        upc
cost              status

What I'm trying to do is use a join function to show all the data as they relate to each other, except the problem is that when I run 我想做的是使用联接函数来显示彼此相关的所有数据,除了问题是当我运行时

SELECT *
FROM table1 a
INNER JOIN table2 b
ON  a.someColumn = b.otherColumn
INNER JOIN table3 c
ON b.anotherColumn = c.nextColumn

It just puts the tables side by side, If I run 它只是将桌子并排放置,如果我运行

SELECT *
FROM table1 a
INNER JOIN table2 b
USING(item_number)

It works but only joins the item number (i have no idea how to use multiple fields such as description which repeats), and for some reason I can only use the two tables when I try to add a third table (most likely being done completely wrong) 它有效,但仅加入项目编号(我不知道如何使用多个字段,例如重复的说明),由于某种原因,当我尝试添加第三个表时,我只能使用两个表(很可能已完全完成)错误)

SELECT *
FROM table1 a
INNER JOIN table2 b
INNER JOIN table3 c
USING(item_number)

I just get a syntax error. 我只是语法错误。

Thanks for all the help in advance 预先感谢您的所有帮助

UPDATE: 更新:

I got it working 我知道了

SELECT *
FROM master_list a 
INNER JOIN bby_report ab USING (item_number, description)
INNER JOIN sales_report b USING (item_number)

Is there a way I can exclude the description from one of the tables and keep it from another one? 有没有办法我可以从一个表中排除描述,而从另一个表中保留它呢? Turns out the descriptions are not an exact match from one table to a another so it keeps returning zero results. 事实证明,从一张表到另一张表的描述并不完全匹配,因此它始终返回零结果。

So to clarify keep description from table1 and leave out description from table2 while being able to JOIN the fields based on item_number 因此,要澄清保留表1中的描述,并保留表2中的描述,同时又可以基于item_number联接字段

SELECT *
FROM master_list a 
INNER JOIN bby_report ab USING (item_number, description)
INNER JOIN sales_report b USING (item_number)

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

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