求一个 SQL:MySQL 字段合并
数据库里面有两个字段,手机号和座机号,现在对两个字段进行合并,规则如下,求个SQL。
手机号(telephone) | 座机(phone) | 说明 |
---|---|---|
18888888888 | 0551-8686895 | 保留手机号(18888888888) |
18888888888 | 也可能汉字 | 保留手机号(18888888888) |
18888888888 | 16666666666 | 优先保留手机号(18888888888) |
为空或者为null则不处理 | ||
0551-1234567 | 18888888888 | 保留手机号(18888888888) |
0551-1234567 | 0551-7654321 | 两个都是座机号,则保留座机的值(0551-7654321) |
将数据合并到手机号(telephone)字段,在数据处理完成后,保留手机号(telephone)字段,删除座机(phone)字段。
谢谢!
写程序处理就行了啊,把你的规则用代码实现一下
mysql 的 if
mysql 中有一个函数 group_concat() , 可以把多个字断的值 合并成一个字符串,刚好满足你的要求
drop temporary table if EXISTS temp; create temporary table if not EXISTS temp SELECT id,telephone,phone, (case when LOCATE('-',telephone) > 0 then case when LOCATE('-',phone) > 0 then phone else phone end else telephone end ) as end_phone FROM
test1
;update test1,temp set test1.telephone = temp.end_phone,test1.phone = null where test1.id = temp.id;