简体   繁体   中英

Make a table from from table A and table B where match

MySQL noob here. I need to create a product catalog table from two others. Table A has a product weight and Table B has description. So I need to make a Table C with the weight and description of course.

These catalogs are different, from different sources, and the only field I can match is SKU.

Should I make Table C from a copy of Table A (with weight), then add a description column, then:

update tableC  
join tableA on tableB.sku = tableA.sku  
set description = tableB.description  

Creating another table to copy and hold values related to each other from two other tables is a poor approach. SQL is a relational language; you should leverage that to simply get both values from both tables:

SELECT
   tableA.weight
 , tableB.description
FROM tableA
LEFT JOIN tableB on tableB.sku = tableA.sku

No need to create a copy to relate the two in a whole other table. That's unnecessary complexity.

Maybe

INSERT INTO
    TableC (`weight`,`desc`)
SELECT
    TableA.weight, TableB.desc
FROM
    TableA
    INNER JOIN
        TableB ON TableA.id = TableB.id;

?

(see http://dev.mysql.com/doc/refman/5.0/en/insert-select.html )

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