如何合并 MySQL 中的两个表,其中表 1 是主表

发布于 2024-11-14 09:44:33 字数 1453 浏览 3 评论 0原文

MySQL 中如何合并两个表? 我看过关于这个主题的其他几篇文章,但它们对我来说没有提供足够的细节。

我是 MySQL 新手,所以请耐心等待,我有一个主表和一个临时表,如下所示:

创建表temp_import (id int(11) NOT NULL 自动增量,
名称 varchar(255) 默认 NULL,
MerchantID int(11) 默认 NULL,
SKU varchar(255) 默认 NULL,
主键 (id) ) ENGINE=MyISAM 自动增量=765811 默认值 CHARSET=utf8;

我将数据插入到临时表中,如下所示:

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

每个表的表格式相同,但数据随着时间的推移而变化。有些商品的数据会发生变化,但 SKU 保持不变。某些商品将不再提供,需要从数据库中删除。

示例:

当前数据库表:

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

新数据库表:

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

最终结果应

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

删除chickenfood,添加Frog,更新cat。

此外,这还需要尽可能高效。我将处理一些大文件。它可以是仅限 MySQL 的代码。

http://www.xaprb。 com/blog/2006/02/21/flexible-insert-and-update-in-mysql/ 我已经看过这个,但它超出了我的想象,当我尝试这样做时它没有 工作...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千纸鹤带着心事 2024-11-21 09:44:33

将 SKU 设为主键。从那里您可以执行 INSERT ... SELECT ... ON DUPLICATE KEY UPDATE 查询。

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

苏大泽ㄣ 2024-11-21 09:44:33

不确定这是否适合您的需求,但似乎您可能需要 查看您的数据。

数据库视图被称为“虚拟表”,它允许您查询其中的数据。

您可以在此处找到一些有关视图的精彩信息。我希望有所帮助,当您需要以不同的方式呈现数据时,视图非常强大并且值得研究。

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.

丢了幸福的猪 2024-11-21 09:44:33

我想我会发布我最终用于此目的的内容。这可能不是“最好”的方式,并且删除该列是完全可选的。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文