【发布时间】:2016-10-21 03:36:51
【问题描述】:
我有一个表需要迁移到新表。
表 A 迁移到表 B
表 A
| ID | type |
| A1 | A |
| A2 | B |
| A3 | A |
| A4 | both |
| A5 | A |
我希望表 B 看起来像这样
表 B
| ID | FKA | TYPE |
| B1 | A1 | Aa |
| B2 | A2 | Bb |
| B3 | A3 | Aa |
| B4 | A4 | Aa |
| B5 | A4 | Bb |
| B6 | A5 | Aa |
如果你意识到如果type是both,它会从表A向表B插入两次。
**FKA 是表 A 的外键
目前我做了 3 个查询
查询 1:
insert into tableB
select sequence1.nextVal, ID,
(case
when type = 'A' then 'Aa'
when type = 'B' then 'Bb'
when type = 'both' then 'Aa'
else NULL
end
) from tableA
查询 2
insert into tableB
select sequence1.nextVal, ID,
(case
when type = 'both' then 'Bb'
else 'laterdelete'
end
) from tableA
查询 3
delete from tableB where type = 'laterdelete'
谢谢大家
【问题讨论】:
-
哪个rdbms?我认为您可以使用映射表或嵌入在模拟映射表的查询中的子查询。
-
我想我使用 oracle RDBMS。如果我的理解是正确的。