简体   繁体   English

最好的MySQL方式将数据从表移动到另一个带结点的方法

[英]Best MySQL way to move data from table to another one with junction

I have a SQL table called members 我有一个名为members的SQL表

| member_id | first_name | last_name | email                      | password | phone_number | country |
|-----------|------------|-----------|----------------------------|----------|--------------|---------|
| 1         | John       | Doe       | john.doe@rambo.vid         | 123456   | 654789159    | en_US   |
| 2         | Jules      | Ferry     | jules.ferry@politician.old | 456789   |              | fr_FR   |

I would like to create a new table users and move data with user_id as junture like this : 我想创建一个新表users并使用user_id作为junture移动数据,如下所示:

users 用户

| user_id | first_name | last_name | email                      | password |
|---------|------------|-----------|----------------------------|----------|
| 1       | John       | Doe       | john.doe@rambo.vid         | 123456   |
| 2       | Jules      | Ferry     | jules.ferry@politician.old | 456789   |

members 会员

| member_id | user_id | phone_number | country |
|-----------|---------|--------------|---------|
| 1         | 1       | 654789159    | en_US   |
| 2         | 2       |              | fr_FR   |

What is the best way (more efficient execution) in MySQL to modify the existing database ? 在MySQL中修改现有数据库的最佳方法(更高效的执行)是什么?

Here is what I am thinking: 这就是我的想法:

INSERT INTO users ( user_id, member_id, first_name, last_name, email, password)
    SELECT <user_id>, -- Wherever this needs to come from or remove if its generated.
         member_id, first_name, last_name, email, password
    FROM members;

Followed by: 其次是:

UPDATE members m INNER JOIN users u
    ON m.member_id = u.member_id 
    SET m.user_id = u.user_id

I have added the member_id field in the users table which you don't have. 我在users表中添加了您没有的member_id字段。 If you don't have the option to do that then you can use the other identifiers to join the table. 如果您没有选项,那么您可以使用其他标识符来加入表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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