How do I merge two tables in MySQL and where table 1 is primary

懵懂的女人 提交于 2019-12-23 03:12:40

问题


How do I merge two tables in MySQL? I've looked at several other posts on this topic but they don't go into enough detail for me.

I'm a novice MySQL user, so bear with me I have a primary table and a temp table that look like this:

CREATE TABLE temp_import ( id int(11) NOT NULL auto_increment,
Name varchar(255) default NULL,
MerchantID int(11) default NULL,
SKU varchar(255) default NULL,
PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=765811 DEFAULT CHARSET=utf8;

I'm inserting data into the temp table like this:

LOAD DATA LOCAL INFILE \'29762.txt\' REPLACE INTO TABLE temp_import FIELDS TERMINATED BY \'|\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' (Name, MerchantID, SKU);

The table format of each table is the same, but the data changes over time. Some items the data chances but the SKU remains constant. Some items will no longer be offered and need to be removed from the database.

example:

Current Database Table:

1, dog, 101, dog101
2, cat, 101, cat101
3, chicken, 102, chicken100
4, chicken food, 102, chicken101

New Database Table:

1, dog, 101, dog101
2, cat, 102, cat101
3, chicken, 102, chicken100
5, Frog, 103, frog101

Final Result Should be

1, dog, 101, dog101
2, cat, 102, cat101
3, chicken, 102, chicken100
5, Frog, 103, frog101

Deleted chickenfood, added Frog, updated cat.

Also this needs to be as efficient as possible. I'll be working with some huge files. And it can be MySQL only code.

http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/ I've looked over this but it's over my head, when I try to do this it doesn't work...


回答1:


Make the SKU the primary key instead. From there you can just do an INSERT ... SELECT ... ON DUPLICATE KEY UPDATE query.




回答2:


Not sure this will fit your needs, but it seem you may need a view for your data.

A database View is known as a "virtual table" which allows you to query the data in it.

You can find some excellent information on views right here. I hope that helps, views are very powerful and worth looking into when you need a different presentation of your data.




回答3:


I thought I would post what I eventually used for this. It's probably not the "BEST" way, and dropping the column is completely optional.

ALTER TABLE `temp_import`
ADD `deleteme` tinyint NOT NULL
DEFAULT 0; -- If you haven't already added a deleteme column 


UPDATE `temp_import` SET  `deleteme` = 1; -- Set the delete field 

LOAD DATA LOW_PRIORITY LOCAL INFILE "import.csv" REPLACE INTO TABLE `temp_import`  FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\n"  IGNORE 1 LINES (`id`, `name`, `m_id`, `sku`);

DELETE FROM `temp_import` WHERE  `deleteme` = 1;

ALTER TABLE `tests` DROP COLUMN `deleteme` -- Optional


来源:https://stackoverflow.com/questions/6274576/how-do-i-merge-two-tables-in-mysql-and-where-table-1-is-primary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!