Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL INSERT INTO WITH multiple SELECT

I'm looking for an SQL Statement to achieve the following:

I have a table that is like {ida | idb | count | direct}
I want to insert rows into the table taking the ids a and b from another table, count and direct are given values. I tried something like this:

Not Working:

INSERT INTO my_tbl (ida, idb, count, direct)  
SELECT id FROM other_tbl WHERE word ='test'  
UNION  
SELECT id FROM other_tbl WHERE word ='tost' 23, 5;

Thanks in advance.

like image 370
Matthias Schroer Avatar asked Jun 23 '26 10:06

Matthias Schroer


1 Answers

Is this what you want?

INSERT INTO my_tbl (ida, idb, count, direct) 
    SELECT o1.id, o2.id, 23, 5
    FROM other_tbl o1 JOIN
         other_tbl o2
         ON o1.word = 'test' AND o2.word = 'tost';
like image 179
Gordon Linoff Avatar answered Jun 25 '26 01:06

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!