0

我有一个问题,我想使用 2 select 插入新表,但我不知道这是否可能......

我的代码:

insert INTO attributs_libres_lignes VALUES  ( SELECT LIGN_PKID, "INSERT HERE" from ATTRIBUTS_LIGNES a, "HERE THE NUMBER OF COLUMN OF PKID (1 to 4)", "HERE A DATE");
select COLONNE01 from attributs_lignes
union all select COLONNE02 from attributs_lignes
union all select COLONNE03 from attributs_lignes
union all select COLONNE04 from attributs_lignes

我想在第一部分插入第二部分(选择 COLONNE01 ....)而不是“在此处插入”

数据旧表:第 1 列 ("PKID1", one","two","three") 第 2 列 ("PKID2","one","two", "three")

新表:

(PKID1, 一, 1) (PKID2, 一, 1) (PKID1, 二, 2) (PKID2, 二, 2) (PKID1, 三, 3) (PKID2, 三, 3)

-

4

3 回答 3

0

您可以按如下方式使用 CROSS JOIN:

INSERT INTO ATTRIBUTS_LIBRES_LIGNES
SELECT A.LIGN_PKID,
       C.COL_ATTRIBUTES
  FROM ATTRIBUTS_LIGNES A
 CROSS JOIN (
    SELECT COLONNE01 COL_ATTRIBUTES FROM ATTRIBUTS_LIGNES UNION ALL
    SELECT COLONNE02 FROM ATTRIBUTS_LIGNES UNION ALL
    SELECT COLONNE03 FROM ATTRIBUTS_LIGNES UNION ALL
    SELECT COLONNE04 FROM ATTRIBUTS_LIGNES
) C
于 2020-11-04T10:37:04.443 回答
0

不就是这么简单吗?

insert into attributs_libres_lignes
select lign_pkid, colonne01 from attributs_lignes union all
select lign_pkid, colonne02 from attributs_lignes union all
select lign_pkid, colonne03 from attributs_lignes union all
select lign_pkid, colonne04 from attributs_lignes;
于 2020-11-04T10:54:16.850 回答
0

我建议cross apply取消透视。这比 更有效union,因为它只扫描一次表,而不是每个成员子查询一次。

所以:

insert into attributs_libres_lignes (lign_pkid, colonne)
select a.lign_pkid, v.colonne
from attributs_lignes a
cross apply (
    select a.colonne01 colonnefrom dual
    union all select a.colonne02 from dual
    union all select a.colonne03 from dual
    union all select a.colonne04 from dual
) v

insert请注意,枚举语句的目标列是一种很好的做法。我假设目标表的列是lign_pkidcolonne,您可能需要查看它。

于 2020-11-04T10:55:42.410 回答