2017-08-04 80 views
0

我想複製第三列和第四列,但更改第二列。我該怎麼辦?我想知道如何編寫sql查詢。謝謝。如何複製一些列並同時更改一列?

例如:

表1:

1, 1, aaa, bbb 

2, 1, ads, bff 

3, 1, awq, bcc 

,我想table1的最後是:

1, 1, aaa, bbb 

2, 1, ads, bff 

3, 1, awq, bcc 

4, 2, aaa, bbb 

5, 2, ads, bff 

6, 2, awq, bcc 

(第一列是ID)

+0

,問題是什麼? – Jens

+0

請添加一些代碼,顯示您已經嘗試過的內容。 – EFrank

+0

我想複製第三列和第四列,但更改第二列。我該怎麼辦? –

回答

0

這應該工作:

insert into table1 (column2,column3,column4) 
    select 2,column3, column4 
    from table1 where column2 = 1 
+0

謝謝!有用。 –

+0

@DanniChen不用客氣 – Jens

0

你的建議:

select (@cnt:[email protected] + 1) as RowNumber,a.c1,a.c2,a.c3 from 
(select 1 as id, 1 as c1, 'aaa' as c2, 'bbb'as c3 union all 
select 2, 1, 'ads', 'bff' union all 
select 3, 1, 'awq', 'bcc' 

union all 

select 1 as id, 1 as c1, 'aaa' as c2, 'bbb'as c3 union all 
select 2, 1, 'ads', 'bff' union all 
select 3, 1, 'awq', 'bcc') as a 

cross JOIN 

(select @cnt:=0) as tmp 

結果:

1 1 aaa bbb 
2 1 ads bff 
3 1 awq bcc 
4 1 aaa bbb 
5 1 ads bff 
6 1 awq bcc 
相關問題