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

时间:2015-06-02 09:40:44

标签: mysql database join sql-update

我有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          | 

我需要使用ak_class_object表中的class_id和ak_class表中的object_id填充ak_objects

问题是如何更新(我需要更新,因为当前存在一些错误的数据)ak_class表中的class_id包含所有ID?我正在考虑将它与JOIN一起使用我不知道要使用哪个id来加入它们,因为class_id只是要更新

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$$

1 个答案:

答案 0 :(得分:0)

我正在编写通用语法,根据您的要求更改表名和列名。

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