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

sbdsn5lh  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(483)

我有一个包含phone的列名phone1#

0345123456,032145678
0345221123,032443332
0347886543,038875532
0345776767

例如,我必须在另一个列名称phone 2中将phone#隔在“,”之后

0345123456 in  phone1 
032145678  in  phone2
iswrvxsc

iswrvxsc1#

使用子字符串索引可以根据分隔符来显示字符串

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

只有当你知道要分隔多少时,这才适用

mzsu5hc0

mzsu5hc02#

从样本数据,我觉得你需要以下

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 '%,%'
sz81bmfz

sz81bmfz3#

试试这个:

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 '%,%'

相关问题