MySQL: Insert row on table2 if row in table1 exists
Posted
by Andrew M
on Stack Overflow
See other posts from Stack Overflow
or by Andrew M
Published on 2010-05-05T01:14:25Z
Indexed on
2010/05/05
1:18 UTC
Read the original article
Hit count: 260
mysql
|mysql-query
I'm trying to set up a MySQL query that will insert a row into table2 if a row in table1 exist already, otherwise it will just insert the row into table1.
I need to find a way to adapt the following query into inserting a row into table2 with the existing row's id.
INSERT INTO table1 (host, path) VALUES ('youtube.com', '/watch') IF NOT
EXISTS ( SELECT * FROM table1 WHERE host='youtube.com' AND path='/watch' LIMIT 1);
Something kind of like this:
INSERT ... IF NOT EXISTS(..) ELSE INSERT INTO table2 (table1_id) VALUES(row.id);
Except I don't know the syntax for this.
© Stack Overflow or respective owner