是否可以使用 LOAD DATA INFILE 类型命令来更新数据库中的行?

发布于 2024-08-06 22:12:29 字数 492 浏览 6 评论 0原文

伪表:

 | primary_key | first_name | last_name | date_of_birth |
 | 1           | John Smith |           | 07/04/1982    |

目前 first_name 包含多行的用户全名。期望的结果是分割数据,因此first_name包含“John”,last_name包含“Smith”。

我有一个 CSV 文件,其中包含所需的数据格式:

 | primary_key | first_name | last_name |
 | 1           | John       | Smith     |

有没有办法使用 LOAD DATA INFILE 命令处理 CSV 文件,以使用 Primary_key 更新此表中的所有行 - 并且在执行期间不替换该行中的任何其他数据过程(即出生日期)?

Pseudo table:

 | primary_key | first_name | last_name | date_of_birth |
 | 1           | John Smith |           | 07/04/1982    |

At the moment first_name contains a users full name for many rows. The desired outcome is to split the data, so first_name contains "John" and last_name contains "Smith".

I have a CSV file which contains the desired format of data:

 | primary_key | first_name | last_name |
 | 1           | John       | Smith     |

Is there a way of using the LOAD DATA INFILE command to process the CSV file to UPDATE all rows in this table using the primary_key - and not replace any other data in the row during the process (i.e. date_of_birth)?

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

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

发布评论

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

评论(2

放血 2024-08-13 22:12:29

在这种情况下,我通常LOAD DATA INFILE到具有相同结构的临时表。然后,我使用 ON DUPLICATE KEY UPDATE 从临时表到真实表执行 INSERT 。这允许在不破坏真实表的情况下进行数据类型检查;它相对较快,并且不需要摆弄您的 .csv 文件。

In this situation I usually LOAD DATA INFILE to a temp table with identical structure. Then I do INSERT with ON DUPLICATE KEY UPDATE from the temp table to the real table. This allows for data type checking without wrecking your real table; it's relatively quick and it doesn't require fiddling with your .csv file.

樱花坊 2024-08-13 22:12:29

不会。虽然 LOAD DATA INFILE 有一个 REPLACE 选项,它实际上会替换有问题的行 - 即删除现有行并插入新行。

如果您将 LOAD DATA INFILE 配置为仅插入某些列,则所有其他列都将设置为其默认值,而不是它们当前包含的值。

您可以修改 CSV 文件以包含一堆 UPDATE 语句吗?通过一些正则表达式替换应该相当简单。

No. While LOAD DATA INFILE has a REPLACE option, it will actually replace the row in question - that is, delete the existing one and insert a new one.

If you configure your LOAD DATA INFILE to only insert certain columns all others will be set to their default values, not to values they currently contain.

Can you modify your CSV file to contain a bunch of UPDATE statements instead? Should be reasonably straightforward via some regex replaces.

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