0

I have cat table in a2 database I want to insert into id, img columns from different tables with different database

INSERT INTO a2.cat (id, img) SELECT id FROM topshop_test.product , SELECT name FROM topshop_test.product-images ;

1 Answer 1

2

I think you need to look into using a JOIN for this:

INSERT INTO a1.cat (id, img)
SELECT p.id, pi.name
FROM topshop_test.product p 
    JOIN topshop_test.product-images pi ON p.id = pi.productid

This assumes the product-images table has a productid field that links to the product table.

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

4 Comments

p and pi is stander or I can type any characters
@FahmyFarahat -- they are just table aliases -- easier than typing the table name out. So yes, you could change those to any values.
dose not work here the error#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-images pi ON p.id = pi.productid' at line 4
@FahmyFarahat -- You need to change pi.productid to whatever your foreign key is in that table (you didn't supply table definitions, so I had to guess). You should have a product id field in your product-images table -- use that instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.