如何复制多行,但用mysql更改一个字段

时间:2011-04-14 13:43:27

标签: mysql

说我有一张表如:

val cols cole
1   1    3
2   1    5
4   1    9
2   6    8
3   1    9
1   4    9
2   9    9

我想要一个复制val = 2的所有行的查询,但是对于这些新行,将val更改为5.

导致:

val cols cole
1   1    3
2   1    5
4   1    9
2   6    8
3   1    9
1   4    9
2   9    9
5   1    5
5   6    8
5   9    9

1 个答案:

答案 0 :(得分:10)

您想要SELECT查询还是INSERT

这是适用于您的SELECT

select val, cols, cole
from your_table
UNION ALL
select 5 as val, cols, cole
from your_table
where val = 2

这是INSERT

insert into your_table (val, cols, cole)
select 5 as val, cols, cole
from your_table
where val = 2