繁体   English   中英

导入两列,然后根据不同的分隔符和第一列将第二列分为许多列和行

[英]Importing two columns and dividing the second column into many columns and rows based on a different delimiter and based on the first column

这是一个艰巨的挑战,我不确定使用MYSQL是否可以实现

假设我有一个包含3列的表格

---------------------------------------------------
| gt3  | sv  | 12.1//1.3//4.5///3.3//3.3//3.4      |
| gt2  | sd  | 13.4//13//12.22///5.5//5.1//5.7     |
| gt1  | eer | 12//33                              |

我正在尝试使用Dbvisualizer将此表导入到数据库中,这应该是这样的:

-------------------------------
| gt3  | 12.1  | 1.3   |  4.5  |
| gt3  | 3.3   | 3.3   |  3.4  |   
| gt2  | 13.4  | 13    | 12.22 |
| gt2  | 5.5   | 5.1   | 5.7   |
| gt1  | 12    | 33    |       |

所以我忽略了第二列,并根据第一列拆分了第三列,所以//是新列,///是新行

我已经试过了

LOAD DATA LOCAL INFILE 'C:\\Users\\youse\\fakemas.txt'
INTO TABLE SA2
FIELDS TERMINATED BY '//'
LINES TERMINATED BY '///'

这只是按照我的意愿正确地拆分了第三列,但没有使用第一列。

好吧,如果您知道可以表示多少行,则可以执行以下操作:

select col1, 
       substring_index(rowvals, '//', 1),
       (case when rowvalues like '%//%' 
             then substring_index(substring_index(rowvals, '//', 2), '//', -1)
        end),
       (case when rowvalues like '%//%//%' 
             then substring_index(rowvals, '//', -1)
        end)
from (select t.*,
             substring_index(substring_index(col3, '///', n.n), '///', -1) as rowvals
      from t join
           (select 1 as n union all select 2) n
           on col3 like concat('%', repeat('///%', n.n - 1))
     ) t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM