【问题标题】:insert multiple rows from select statement in sql从sql中的select语句中插入多行
【发布时间】: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。如果我的理解是正确的。

标签: sql oracle select insert


【解决方案1】:

我假设 rdbms 是 Oracle。您可能希望使用这些值创建一个表(例如“tablemapping”)

 FieldFrom FieldTo
 A         Aa
 B         Bb
 both      Aa
 both      Bb

所以你可以这样做:

 Insert into tableB
 select sequence1.nextval, ID, FieldTo
    FROM tableA a 
         join tablemapping m 
           on a.type=m.fieldFrom

如果您不想有一个映射表,您可以模拟一个。

 Insert into tableb
 select sequence1.nextval, ID, FieldTo
    FROM tableA a 
         join (
               select 'both'  as FieldFrom,  'Ab'  as FieldTo from dual
                Union all
               select 'both'  as FieldFrom,  'Bb'  as FieldTo from dual
                Union all
               select 'A'  as FieldFrom,  'Aa'  as FieldTo from dual
                Union all
               select 'B'  as FieldFrom,  'Bb'  as FieldTo from dual
              ) tablemapping m 
           on a.type=m.fieldFrom

【讨论】:

  • 我不太了解映射表。但我稍后会尝试。但是我的表包含或数百万个查询,会影响性能吗?
  • 可能是(我们必须查看索引和执行计划)。但是,它仍然只是对 tableA 的一次访问和在 tableB 中的一次插入。
  • tablemapping 是桥表吗?我之前想说的是。它会影响我的表现吗,因为原始表中有数百万个数据。虽然我还没有尝试你的方法,会尽快回复你
  • 是的,“tablemapping”只是我给桥表起的名字。为了性能,当你的大表必须被读取一次时,这是我能找到的唯一方法。
  • 首先抱歉迟到了,我最近换了工作单位。感谢您的跟进,非常感谢。当我完全按照您的查询时,它返回插入的 0 行。所以我将 select sequence1.nextval, ID, FieldTo 更改为 select sequence1.nextval, ID, type 并将 a.Id=m.fieldFrom 更改为 a.type=m.fieldFrom 结果是所有 type = BOTH 都输入为 | B4 | A4 |两者| | B5 | A4 |两者|什么时候应该| B4 | A4 |啊 | | B5 | A4 | BB |我是不是查询错了你的sql?
【解决方案2】:

您可以在第一个查询中使用 union 来在 tableB 中获得所需的结果。 查询如下:

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
UNION
select sequence1.nextVal, ID, 
(case
when type = 'A' then 'Aa'
when type = 'B' then 'Bb'
when type = 'both' then 'Bb'
else NULL
end
) from tableA

当 type 都在 tableA 中时,上面的查询将在 tableB 中插入 Aa 和 Bb。 希望这会有所帮助。

【讨论】:

  • 我有点担心对序列的两次调用。它会为同一条记录生成两倍相同的值吗?
  • 我试过这个查询。我猜@Insac 是对的。它将生成两次
  • 出现错误 SQL 错误:ORA-02287:此处不允许序列号
  • 你可以使用 ROW_NUMBER() OVER(ORDER BY ID) 代替序列
  • 不会每次运行查询时都从 1 开始添加吗?
猜你喜欢
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
  • 1970-01-01
  • 2016-09-01
  • 2016-07-02
相关资源
最近更新 更多