Wian Crous
Wian Crous

Reputation: 7

MySQL INSERT With IF

I am trying to write a mysql script that should work as follows but I have no idea how to execute it.

The logic is:

if table1.column1 = table2.column1:
 INSERT INTO table3 (col1,col2)
 VALUES (table1.column2 , table2.column3)

The tables referred to are all in the same database

Upvotes: 0

Views: 37

Answers (1)

forpas
forpas

Reputation: 164099

Instead of VALUES you can use a SELECT statement that joins table1 and table2:

INSERT INTO table3 (col1,col2) 
SELECT t1.column2, t2.column3
FROM table1 t1 INNER JOIN table2 t2
ON t1.column1 = t2.column1

Upvotes: 2

Related Questions