简体   繁体   English

将一个表中的 n 行更新为另一个表中的 n 行(无外键)

[英]Update n rows from a table to n rows in another table (no foreign key)

I have 2 tables, table_1 and table_2 .我有 2 个表, table_1table_2 table_1 included all data which I need to update to table_2 . table_1包括我需要更新到table_2的所有数据。

table_1表格1

column_2 column_2 column_3 column_3
b1 b1 b1 b1
b2 b2 b2 b2

table_2表_2

column_1列_1 column_2 column_2 column_3 column_3 column_4 column_4
1 1 a1 a1 a1 a1 a一个
2 2 a一个 a一个 a一个
2 2 a一个 a一个 a一个
1 1 a2 a2 a2 a2 a一个
2 2 a一个 a一个 a一个

I need to put all data of table_1 to table_2 where column_1 is a specific number, for example, 1. However, I don't have any foreign key to join these two tables.我需要将table_1的所有数据放到table_2中,其中column_1是一个特定的数字,例如 1。但是,我没有任何外键来连接这两个表。 The only relationship is that table_1 has n rows, table_2 also has n rows where column_1 = 1, and I want n rows in table_1 to be updated to these n rows in table_2 .唯一的关系是table_1有 n 行, table_2也有 n 行,其中column_1 = 1,我希望table_1中的 n 行更新为table_2中的这 n 行。 My result would look like this:我的结果如下所示:

column_1列_1 column_2 column_2 column_3 column_3 column_4 column_4
1 1 b1 b1 b1 b1 a一个
2 2 a一个 a一个 a一个
2 2 a一个 a一个 a一个
1 1 b2 b2 b2 b2 a一个
2 2 a一个 a一个 a一个

Any help would be appreciated.任何帮助,将不胜感激。

I think you should try to do that with a scripting language instead of using sql.我认为您应该尝试使用脚本语言而不是使用 sql 来做到这一点。 get everything from table2 where column_1=1 order by column_2 to an array of objects like from table2 where column_1=1 order by column_2到对象数组,例如

[
 {column_1: 1, column_2: b1, column_3: b1, column_4: a},
 {column_1: 1, column_2: b2, column_3: b2, column_4: a}
]

then get everything from table1 order by column_2 in an array of objects然后在对象数组中from table1 order by column_2中获取所有内容

[
 {column_1: b1, column_2: b1},
 {column_1: b2, column_2: b2}
]

and for every element in table1, update table2 using column_1, column_2, column_3, column_4 in a where qlause对于 table1 中的每个元素,使用 where qlause 中的 column_1、column_2、column_3、column_4 更新 table2

I dont think any other way to do this...it really a pain if the structure is like that我认为没有其他方法可以做到这一点……如果结构是这样的,那真的很痛苦

It's unclear by what logic you would like which rows in table1 to update which in table2.目前还不清楚您希望 table1 中的哪些行更新 table2 中的哪些行。 I will assume you just want to go in order: row 1 to row 1, 2 to 2 etc.我假设您只想按顺序 go:第 1 行到第 1 行、2 到 2 等。

What we can do is add a row_number() to each table, then join on that.我们可以做的是为每个表添加一个row_number() ,然后加入它。

I'm not 100% on MySQL syntax but hopefully you should get the idea.我不是 100% 了解 MySQL 语法,但希望你能明白这一点。 See also here for further "update through join" syntaxes:另请参阅此处以获取更多“通过加入更新”语法:

WITH t2 AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn
    FROM table_2
    WHERE column_1 = 1
),
t1 AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn
    FROM table_1
)
UPDATE t2
SET
    t2.column_2 = t1.column_2,
    t2.column_3 = t1.column_3
INNER JOIN t1 ON t1.rn = t2.rn;

If you cannot do an update on a WITH table, then you must self-join table2 .如果您无法对WITH表进行更新,则必须自table2 You haven't indicated the PK of that table, I will just use column PK :您没有指出该表的 PK,我将只使用列PK

UPDATE table_2
SET
    table_2.column_2 = t1.column_2,
    table_2.column_3 = t1.column_3
INNER JOIN (
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn
    FROM table_2
    WHERE column_1 = 1
) AS t2 ON t2.PK = table_2.PK
INNER JOIN (
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn
    FROM table_1
)AS t1 ON t1.rn = t2.rn;

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

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