Skip to content
Advertisement

Multiple inserts of multi-valued subqueries

I found the single insert answer from this question: How can I insert values into a table, using a subquery with more than one result?. However, I’m looking for a way to do multiple inserts in this fashion.

Single-Insert version WORKING! (using the previous answer…):

INSERT INTO prices (group, id, price)
SELECT 7, articleId, 1.50 FROM article WHERE name like 'ABC%';

Multi-Insert version (Failing)

INSERT INTO prices (group, id, price)
(select 7, articleId, 1.50 FROM article WHERE name like 'ABC%'),
(select 9, articleId, 2.50 FROM article WHERE name like 'DEF%');

Is there a method of doing these multiple inserts of multi-column subqueries with a simple-enough syntax?

Thanks!

Advertisement

Answer

You can use UNION ALL. It does a union of the two result sets while keeping duplicates.

INSERT INTO prices
            (group,
             id,
             price)
            SELECT 7,
                   articleId,
                   1.50
                   FROM article
                   WHERE name LIKE 'ABC%'
            UNION ALL
            SELECT 9,
                   articleId,
                   2.50
                   FROM article
                   WHERE name LIKE 'DEF%';
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement