简体   繁体   中英

Selecting columns to insert into table with where condition

I am working on a project where I need to grab the img path from a backup table and insert it in to the image path column of a new table based on if the item names match up perfectly (as the id's vary).

This is what I tried, but getting an error that the items.prod_name column is not found:

MySQL

INSERT INTO items (img_path) 
SELECT img_path
FROM items_backup 
WHERE items.prod_name = items_backup.prod_name

You would seem to want an update , not an insert . The syntax in MySQL is:

update items i join
       items_backup ib
       on i.prod_name = ib.prod_name
    set i.img_path = ib.img_path;

To follow up on my comment:

UPDATE items 
INNER JOIN items_backup ON items.prod_name = items_backup.prod_name
SET items.img_path = items_backup.img_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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