MySQL序列列生成

时间:2018-08-27 09:28:00

标签: mysql

基于其他列的值,我需要有关列生成器的帮助。我有一张桌子,如下。

CREATE TABLE test1
(
    `C1`              INT,
    `C2`             VARCHAR(120),
    `C3`                 VARCHAR(50),
    `C4`                VARCHAR(20),
    `C5`                 VARCHAR(50)
);

Insert into test1 (C1,C2,C3,C4,C5) values (1071,'TEST01','51','464',null);
Insert into test1 (C1,C2,C3,C4,C5) values (1071,'TEST02','9','109',null);
Insert into test1 (C1,C2,C3,C4,C5) values (970,'TEST03','1,2','454,41',null);
Insert into test1 (C1,C2,C3,C4,C5) values (950,'TEST04','1,2','253,255,254',null);
Insert into test1 (C1,C2,C3,C4,C5) values (792,'TEST05','1,2','536','57,65,66');

输出如下:

C1      C2      C3      C4              C5
1071    TEST01  51      464 
1071    TEST02  9       109 
970     TEST03  1,2     454,41  
950     TEST04  1,2     253,255,254 
792     TEST05  1,2     536             57,65,66

现在,选择查询应显示所有记录以及其他列,例如C3_NEW,C4_NEW和C5_NEW。这些新列基于C3,C4和C5的值。

示例:如果C3具有两个值,即1,2,而C4具有454,41 那么C3_NEW应该已经基于1然后是2创建了 和C4_NEW应该基于454和41创建。因此,其他行基于值。

下面是所需的输出,我正在寻找。

C1      C2      C3      C4          C5          C3_NEW  C4_NEW  C5_NEW
1071    TEST02  9       109                     9       109 
1071    TEST01  51      464                     51      464 
970     TEST03  1,2     454,41                  1       41  
970     TEST03  1,2     454,41                  1       454 
970     TEST03  1,2     454,41                  2       41  
970     TEST03  1,2     454,41                  2       454 
950     TEST04  1,2     253,255,254             1       253 
950     TEST04  1,2     253,255,254             1       255 
950     TEST04  1,2     253,255,254             1       254 
950     TEST04  1,2     253,255,254             2       253 
950     TEST04  1,2     253,255,254             2       255 
950     TEST04  1,2     253,255,254             2       254 
792     TEST05  1,2     536         57,65,66    1       536     57
792     TEST05  1,2     536         57,65,66    1       536     65
792     TEST05  1,2     536         57,65,66    1       536     66
792     TEST05  1,2     536         57,65,66    2       536     57
792     TEST05  1,2     536         57,65,66    2       536     65
792     TEST05  1,2     536         57,65,66    2       536     66

在Oracle中,下面的查询给出所需的最终输出:

Select
    C1, C2, C3, C4, C5,
    decode(C3_NEW, 'NONE', NULL, C3_NEW)  C3_NEW,
    C4_NEW,
    regexp_substr(sp_pub_sub.C5,'[^,]+',1,column_value) C5_NEW
from
    (Select C1, C2, C3, C4, C5, C3_NEW,regexp_substr(sp_C3.C4,'[^,]+',1,column_value) C4_NEW
     from
     (select  C1, C2, C3, C4, C5, regexp_substr(C3,'[^,]+',1,column_value) C3_NEW 
     from  (select C1, C2, replace(C5, ' or ',',') as C5, replace(C3, ' or ',',') as C3, replace(C4, ' or ',',') as C4 from test1) t
            ,table(cast(multiset(select level from dual connect by level <= 1+length(C3) - length(replace(C3,',')) ) as sys.OdciNumberList)) ) sp_C3
            ,table(cast(multiset(select level from dual connect by level <= 1+length(sp_C3.C4) - length(replace(sp_C3.C4,',')) ) as sys.OdciNumberList)) ) sp_pub_sub
            ,table(cast(multiset(select level from dual connect by level <= 1+length(sp_pub_sub.C5) - length(replace(sp_pub_sub.C5,',')) ) as sys.OdciNumberList))
 order by 1 desc,2,3,4,5,6;

现在,mysql没有多重集或级别,因此如何在mysql中获得相同的输出。请放心,因为我不是开发人员:-)

预先感谢

0 个答案:

没有答案