Insert data from one table to another

Hi,
can’t seem to wrap my brain around this one. I have 2 tables and i want to insert information from the one table to the other but only where the information doesn’t already exist.

this is what ive been playing with so far. Am i completely off track? any help appreciated.


mysql_query("INSERT INTO table1 (name) SELECT name FROM table2 WHERE table1.name != table2.name") 


thanks for that it worked perfectly apart from having to change one small bit as it said it was ambiguous - just added table2.name in the select part.


INSERT INTO table1 (name) 
SELECT 
    table2.name 
FROM table2 
LEFT OUTER JOIN table1 AS t1
ON t1.name = table2.name
WHERE t1.name IS NULL

Sorry didn’t see the mysql specific forum, i’ll keep an eye out in future.

thanks again

Try this:


INSERT INTO table1 (name) 
SELECT 
    name 
FROM table2 
LEFT OUTER JOIN table1 AS t1
ON t1.name = table2.name
WHERE t1.name IS NULL

And next time please post the error message you get when your query fails. It helps us understand the problem.

And did you know there is a dedicated MySQL subforum here? :slight_smile: