MySQL:在“update”和“where”字段中使用select的结果

时间:2013-01-23 12:53:21

标签: mysql sql

我有两个图像表,现在我想将所有图像打包在一个表中并通过外键fk_image引用它:

另外:

Table1: ('id_system' , 'logo_image_raw', 'fk_image')
Table2: ('id_image', 'image_raw_data')

如何将logo_image_raw复制/移动到Table的2'image_raw_data'并在fk_image中反映其id_image?

2 个答案:

答案 0 :(得分:1)

只需INSERT...INTO SELECT

INSERT  INTO table2 (id_image, image_raw_data)
SELECT  fk_image, logo_image_raw
FROM    table1 

如果列image_raw_data为空但您已经id_image,则需要加入表格,以便获取每个ID的特定图像,

UPDATE  table2 b 
        INNER JOIN table1 a
           ON a.fk_image = b.id_image
SET     b.image_raw_data = a.logo_image_raw

答案 1 :(得分:1)

你可以这样做..

 INSERT INTO table2
 SELECT table1.fk_image, table1.logo_image_raw
 FROM table1, table2 WHERE table1.fk_image = table2.id_image