如何在同一表中将数据从一列拆分到另一列?

时间:2018-09-05 06:12:27

标签: mysql sql

我的列名phone1包含电话号码

0345123456,032145678
0345221123,032443332
0347886543,038875532
0345776767

我必须在另一个列名称电话2中的“,”之后分隔电话号码 例如

0345123456 in  phone1 
032145678  in  phone2 

3 个答案:

答案 0 :(得分:0)

在我看来,您需要以下示例数据

   select
    SUBSTRING_INDEX(phone#, ",", -1) as phone1,        
    SUBSTRING_INDEX(phone#, ",", 1) as phone2 from table1
    where phone# like '%,%'
    union all
    select phone# as phone1,  '' as phone2  fraon table1  
     where phone# not like '%,%'

答案 1 :(得分:-1)

使用substring_index,您可以基于分隔符来混合字符串

substring_index(`#phone`,',',1) as phone1,substring_index(`#phone`,',',-1) as phone2

这只有在您知道要定界多少时才适用

答案 2 :(得分:-1)

尝试一下:

  SELECT concat(Substring(phone, 1, Locate(',', phone) - 1),' in phone1') AS 
            phone1, 
           concat(Substring(phone, Locate(',', phone) + 1),' in phone2')   AS phone2
    FROM   tablename where phone not like '%,%'