1

I have two tables, one that stores the the id of the an old img

 id | old_img_name | old_img_id 

and another table that stores the info of the current img

id |img_name | img_w | img_h | etc.... 

and im trying to create a query to update(switch) the info from the old table into the new one, the only field that I want to update is the img_name.

I have create two queries to do that :

SELECT id, old_img_id, old_img_name FROM `img_archives` WHERE id = 3 UPDATE imgs SET img_name = old_img_name WHERE id = old_img_id 

I want to convine these two queries into one, but I'm having a lot or problem doing it.

I have tried this:

UPDATE imgs SET img_name = img_archives_old_img_name FROM imgs INNER JOIN img_archives ON imgs_id = img_archives.old_img_id; 

not is not working

3
  • Why is it not working? What part is not working? Commented Feb 20, 2019 at 20:47
  • Also if you want to update the image name from one table to another why is the img_name hardcoded to test? Commented Feb 20, 2019 at 20:47
  • Because that were I'm lost, i don't know how to get the name from the other table in one query Commented Feb 20, 2019 at 20:48

3 Answers 3

2
UPDATE imgs i INNER JOIN img_archives ia ON i.imgs_id = ia.old_img_id SET i.img_name = ia.old_img_name; 

More info is here: http://www.mysqltutorial.org/mysql-update-join/

Sign up to request clarification or add additional context in comments.

Comments

1
-- this should do it UPDATE imgs SET img_name = img_archives.old_img_name FROM imgs INNER JOIN img_archives ON imgs_id = img_archives.old_img_id; 

Comments

1

In MySQL the update with join should use this syntax. If you want update the id = 3

UPDATE imgs INNER JOIN img_archives ON imgs.imgs_id = img_archives.old_img_id AND img_archives.id = 3 SET img_name = "test" 

1 Comment

Im trying your solution and I keep getting this error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM imgs INNER JOIN

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.