来自其他行结果集的SQL副本

时间:2014-03-26 02:28:04

标签: sql

有没有办法将结果集从select查询复制到同一个表中的另一个结果集?

这有什么标准?我需要有相同数量的列和行才能执行此操作吗?

我不确定下面的内容是否有效:

UPDATE t1
SET t1.source_id = t2.source_id, t1.selected_team_id = t2.selected_team_id, t1.margin = t2.margin
FROM (
        SELECT tip_id, source_id, selected_team_id, margin, tipper_id
        FROM tip
        WHERE tip_id >= 26650287 AND tip_id <= 26650295
    ) t1, 
    (
        SELECT tip_id, source_id, selected_team_id, margin, tipper_id
        FROM tip
        WHERE tip_id >= 26650289 AND tip_id <= 26650299
    ) t2

示例数据:

tip_id      source_id    selected_team_id   margin   tipper_id
26650287    8            2                  0        217630
26650288    8            11                 0        217630
26650289    8            6                  0        217630
26650290    8            9                  0        217630
26650291    8            1                  0        217630
26650292    8            17                 0        217630
26650293    8            8                  0        217630
26650294    8            4                  0        217630
26650295    8            3                  21       217630
26650296    0            21                 0        381719
26650297    0            20                 0        381719
26650298    0            24                 0        381719
26650299    0            26                 0        381719

1 个答案:

答案 0 :(得分:0)

update t1,(select * from t1 where tip_id >= 26650289 AND tip_id <= 26650299) as t2
set t1.source_id = t2.source_id, t1.selected_team_id = t2.selected_team_id, t1.margin = t2.margin
where (t1.tip_id >= 26650287 AND t1.tip_id <= 26650295) and (t1.tip_id + 2 = t2.tip_id)