Reputation: 2069
I'm trying to insert into a relational table with the result of two SELECT queries.
table 1:
id | url
-------------------------
1 | http://something.com
table 2:
id | address
----------------------------
1 | [email protected]
table 3:
table1_id | table2_id
---------------------
1 | 1
And I'm trying to build a INSERT query combined out of two SELECTs and a UNION
I've been working with this:
INSERT INTO table3
SELECT id FROM table1
WHERE table1.url = 'http://something.com'
UNION
SELECT id FROM table2
WHERE table2.address = '[email protected]';
Where have I gone wrong with this? I'm getting an error on the second SELECT, it is able to SELECT the first ID and pass it into the INSERT, but it's trying to insert (1, null) even though the second SELECT is valid on its own.
Upvotes: 0
Views: 529
Reputation: 48207
INSERT INTO table 3
select A.id, B.id
from Table1 A
cross join Table2 B
where A.url = 'http://something.com'
and B.address = '[email protected]'
Upvotes: 1