简体   繁体   English

用MySQL中另一张表中的数据更新一列中的许多行

[英]UPDATE in many rows one column with data from another table in MySQL

Suppose I have a table1 like below: 假设我有一个如下的table1:

CREATE TABLE IF NOT EXISTS table1
(
table1_id INT NOT NULL, 
field1 INT DEFAULT NULL, 
field2 INT DEFAULT NULL,

FOREIGN KEY (field1) REFERENCES fields(field_id),
FOREIGN KEY (field2) REFERENCES fields(field_id),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

and a table2: 和一个table2:

CREATE TABLE IF NOT EXISTS table2
(
table2_id INT NOT NULL,
another_id INT NOT NULL,
field1 INT DEFAULT NULL,

PRIMARY KEY (table2_id , another_id),
FOREIGN KEY (another_id) REFERENCES table1(table1_id),
FOREIGN KEY (field1) REFERENCES fields(field_id),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

I have filled with data the columns table1_id , field1 , field2 of table1 and table2_id , another_id of table2. 我已经充满了列数据table1_idfield1field2的表1和table2_idanother_id表2中。

What I want to do is fill with data the field1 column of table2. 我想做的是用表2的field1列填充数据。 The thing is I want that to be either value field1 from table1 or field2 from table1. 问题是我想要的是任一值field1从表1或field2从表1。 I want that to be random. 我希望这是随机的。 If though one of the them is NULL select the other, if both are, just put NULL. 如果它们中的一个为NULL,则选择另一个(如果两者均为),则只需输入NULL。

I can simply create a few UPDATES queries and do it right?. 我可以简单地创建一些UPDATES查询,对吗? Unfortunately I cant cause the table2 has more than 5k rows and I can do 5k UPDATE queries. 不幸的是,我无法导致table2的行超过5k,并且我可以执行5k UPDATE查询。 Moreover am not exactly sure of how to make the MySQL choose at random one of the two fields of table1 as described above. 此外,还不确定如何如上所述使MySQL随机选择table1的两个字段之一。 Let me give you below an example query of how it would be. 让我在下面为您提供一个查询示例。

(I dont know how to select at random columns and with conditions so I'll leave that out in my query - Suppose that out of the random condition the result was to select field1) (我不知道如何在随机列和条件下进行选择,因此我将其保留在查询中-假设在随机条件下,结果是选择field1)

UPDATE table2 SET table2.field1 = (SELECT table1.field1 FROM table1 WHERE table1_id = another_id) WHERE another_id = X;

I'd have to repeat the above for many different X's. 我必须对许多不同的X重复上述操作。

So the question is how to fill the field1 of table2 with either field1 or field2 from table1 with the conditions described above for many X values? 因此,问题在于如何使用上述针对许多X值描述的条件,用field1 or field2 from table1填充field1 of table2 field1 or field2 from table1

To randomly select field1 or field2 , or whichever one is not NULL : 要随机选择field1field2 ,或不为NULL任何一个:

UPDATE table2 SET table2.field1 = (
    SELECT
        CASE
            WHEN ( table1.field1 IS NULL AND table1.field2 IS NULL ) THEN NULL
            WHEN ( table1.field1 IS NULL ) THEN table1.field2
            WHEN ( table1.field2 IS NULL ) THEN table1.field1
            ELSE IF( RAND() < 0.5, table1.field1, table1.field2 )
        END CASE
    FROM table1 WHERE table1_id = another_id
);

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

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