SQL插入多行-一个值更改-行数是动态的

  • 本文关键字:-行 动态 一个 插入 SQL mysql
  • 更新时间 :
  • 英文 :


我需要做一个插入,其中2个值将保持不变,第三个值将改变。所以,像下面这样:

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

预期的结果:

<表类>column_acolumn_bcolumn_ctbody><<tr>1340918771014409187710

MySQL 8.0增加了一种新的语句: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 |
+----------+----------+-----------+

如果你使用的MySQL版本不支持VALUES语句,你可以使用以下语法:

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

最新更新