我继承了一个表,其中一列是不同表的以逗号分隔的主键列表:
id | other_ids | value ---|-----------|------- 1 | a,b,c | 100 2 | d,e | 200 3 | f,g | 3000
我想将此表转换为每个 other_id
都有自己的一列的表:
id | other_id ---|--------- 1 | a 1 | b 1 | c 2 | d 2 | e 3 | f 3 | g
但是,我想不出办法来做到这一点?
该表的大小 > 10 GB,因此如果可能的话,我想在数据库中执行此操作。
最佳答案
第一次发帖,请多多包涵。
试试这个
select id,SUBSTRING_INDEX(other_ids,',',1) as other_id from reverseconcat
UNION
select id,SUBSTRING_INDEX(SUBSTRING_INDEX(other_ids,',',2),',',-1) as other_id from reverseconcat
UNION
select id,SUBSTRING_INDEX(SUBSTRING_INDEX(other_ids,',',3),',',-1) as other_id from reverseconcat
order by id
虽然我真的不能相信任何功劳。在 http://www.programering.com/a/MzMyUzNwATg.html 上找到这个
不确定您将如何处理庞大的数据集。如果 other_ids > 3
,您还需要添加更多联合关于mysql - 将一列设置值转换为 MySQL 中的一列单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648599/