Copy content from one table to another

Asked

Viewed 55 times

-1

I have a table, a call "Cars" with the columns Carid | image

another table called "Pictures" with the columns picID | Inname

I would like to copy all photos from table "Cars" column "image" to table "picture" column "Inname"

I tried to follow a code already placed here , the code worked without error but the result was:

"No altered line"

UPDATE picture pc 
INNER JOIN cars AS c 
ON c.image = pc.ImName 
SET pc.ImName = c.image;
  • How the relationship between the tables is done cars and pictures?

  • 1

    Just make a INSERT ... SELECT .... If the tables are not related, there is no reason to do Join, let alone do an update to insert records.

1 answer

1


It doesn’t take much to realize that the code you wrote doesn’t make sense to what you want to do.

UPDATE picture pc 
INNER JOIN cars AS c 
ON c.image = pc.ImName 
SET pc.ImName = c.image;

Used the UPDATE being that you want to insert new records; made a JOIN in unrelated tables (or at least you did not comment that there is a foreign key relating them and, if there is one, it was ignored in the code); you made the condition c.image = pc.ImName the image does not exist in picture, then the condition will never be satisfied.

I would like to copy all photos from table "Cars" column "image" to table "picture" column "Inname"

How do you want select data from a table and insert in another, you will have to use the SELECT and the INSERT.

INSERT INTO picture (InName)
SELECT image FROM cars

This will select all records from cars, only the column image and will save on the table picture. If you don’t need all the records, just add a clause WHERE in his SELECT.

  • Muitissimo Thanks, You saved me many hours of work, using your code I was able to put all the photos with their respective Ids, ignoring the Ids that have no photo. INSERT INTO Pictures (Imgname, Carid) SELECT Image, Carid FROM Cars WHERE Image <>''

Browser other questions tagged

You are not signed in. Login or sign up in order to post.