'SQL Insert many rows - one value changes - number of rows is dynamic

I need to do do an insert where 2 values will be constant and the third will change. So, something like the following:

INSERT INTO 
    `example_table`(column_a, column_b,column_c)
SELECT 1, [3,4], 409187710
from `example_table`

Desired Result:

column_a column_b column_c
1 3 409187710
1 4 409187710

Just to be clear the number of values I need to insert into column_b will vary - sometimes I will need to insert 2 values, sometimes I may need to insert 10 values, but columns a and c will always have the same values. How do I perform such an insert?

My question differs from this question in that a sql case statement will not suffice. This is a different kind of problem.



Solution 1:[1]

MySQL 8.0 has a new kind of statement: VALUES.

mysql> create table example_table (column_a int, column_b int, column_c int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into example_table
    -> select 1, column_0, 409187710 from (values row(3), row(4)) as t;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from example_table;
+----------+----------+-----------+
| column_a | column_b | column_c  |
+----------+----------+-----------+
|        1 |        3 | 409187710 |
|        1 |        4 | 409187710 |
+----------+----------+-----------+

If you use a version of MySQL that doesn't support the VALUES statement, you can use this syntax:

mysql> insert into example_table 
    -> select 1, b, 409187710 from (select 3 as b union select 4) as t;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bill Karwin