简体   繁体   English

使用来自另一个表的所有数据更新MySQL中的表

[英]Update a table in MySQL with all data from another table

I have 3 tables: ak_class, ak_objects, ak_class_object ak_class: 我有3个表:ak_class,ak_objects,ak_class_object ak_class:

class_id | class_description | class_name |
1        | some description  | some name  |
2        | some description  | some name  |
3        | some description  | some name  | 

ak_objects: 
object_id | object_description | object_name |
1         | some description   | some name   |
2         | some description   | some name   |
3         | some description   | some name   | 

ak_class_object: 
class_object_id | class_id | object_id  |
1               | 1        | 1          |
2               | 2        | 2          |
3               | 3        | 3          | 

I need to fill in the ak_class_object with a class_id from ak_class table and object_id from ak_objects table. 我需要用ak_class表中的ak_class_objectak_class表中的object_id ak_objects

The question is how can I update (I need to update as there is some wrong data currently) the class_id from the ak_class table with all the ids? 问题是如何更新ak_class表中具有所有ID的class_id(我需要更新,因为当前存在一些错误数据)? I was thinking of using it with JOIN ut I don't know which id to use to Join them as class_id is only to be updated 我正在考虑将其与JOIN一起使用,但我不知道该使用哪个ID来加入它们,因为class_id仅用于更新

UPD: I was trying to do it like this, but it didn't work: UPD:我正在尝试这样做,但是没有用:

DELIMITER $$
DROP PROCEDURE class_object_1$$
CREATE PROCEDURE class_object_1()
BEGIN
DECLARE i INT DEFAULT 0;
WHILE (i < 250000) DO
UPDATE ak_class_object 
SET class_id = SELECT DISTINCT class_id from ak_class, object_id = SELECT DISTINCT class_id from ak_objects;
SET i = i + 1;
END WHILE;
END$$

I am writing the generic syntax, change the table names and column name as per your requirements. 我正在编写通用语法,根据您的要求更改表名和列名。

update table1 inner join table2
on table1.id = table2.fk_id
set table1.column = table1.columnUpdated

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

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