简体   繁体   中英

Join multiple mySQL tables into one table w/ same primary key

I am have multiple mySQL tables that each have the same primary key but different columns. For instance, one table has a bunch of information about engine type and another table has information about transmissions. The VIN is used as the primary key in both tables.

I need to combine these tables into one larger table (the "master table) where the VIN is the primary key followed by all the columns from every table. I am doing this so that I can export a "master table" that contains all the vehicle information, as one table.

I am trying to do this without a bunch of SELECT (col1,col2,col3,....) in order to make it simple and save time, if that's possible.

Thanks!

You have to list all the columns explicitly.

INSERT INTO masterTable (vin, col1, col2, col3, col4, col5, col6, col7, ...)
SELECT t1.vin, t1.col1, t1.col2, t1.col3, t2.col4, t2.col5, t3.col6, t3.col7, ...
FROM Table1 AS t1
JOIN Table2 AS t2 ON t1.vin = t2.vin
JOIN Table3 AS t3 ON t1.vin = t3.vin
...

我建议枚举所有项目,但是如果您想从其他表中排除VIN并且仅使用一次,则可以检查此解决方案。 选择MySQL中除一列以外的所有列?

If i understand you correctly you need just simple INNER JOIN sintax... try something like:

SELECT table1.colName, table1.colName2, table1.colNameN, table2.colName, table2.colName2, table2.colNameN
FROM table1
INNER JOIN table2
ON table1.VIN = table2.VIN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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