0

我有具有以下结构和数据的员工表。

id       name       empDetails                                                 phone
1        John       phone:7845982563,pinCode:7895645,subCode:22    
2        pitt       phone:7889451524,pinCode:2565479,subCode:24
3        ken        empId:3243,phone:283241524,pinCode:2563479,subCode:25  
4        rancho     empId:3263,phone:454424154,pinCode:5565479,subCode:24  
.         .                 .   

Sometimes phone poistion will be after empId also.

现在我想检查列 empDetails

如果是,则有字符串电话,然后获取电话号码并将其插入同一行的新电话列中。

我们可以通过这种方式检查记录是否包含字符串电话。

SELECT employee.empDetails FROM employee WHERE  employee.empDetails LIKE '%phone:%'

但是我们如何检索电话号码并将其插入另一列以获得相同的记录?

谢谢,雅利安

4

3 回答 3

2
update  employee.empDetails set phone = cast(replace(empDetails, 'phone:', '') as unsigned) 
where employee.empDetails LIKE '%phone:%'

假设电话号码总是以“电话:”为前缀,这
不是一个干净的答案。但会成功的:)

于 2013-07-24T13:09:36.307 回答
1
SELECT replace(SUBSTRING_INDEX(empDetails,',',1),'phone:','') as phone 
FROM Table1 
where empDetails REGEXP '[[:<:]]phone[[:>:]]'; 

在这里检查工作小提琴: http ://sqlfiddle.com/#!2/802d2/18

请听从 spaceman817 的建议,使用单独的列。

希望这会有所帮助。

〜K


假设您的手机可以在任何位置开始,中间和结束。例如:

(1, 'John', 'phone:7845982563,pinCode:7895645,subCode:22'),
(2, 'pitt', 'pinCode:2565479,subCode:24,phone:7889451524'),
    (3, 'vk', 'pinCode:2565479,phone:7889451524,subCode:24');

使用以下查询:

SELECT  
replace(SUBSTRING_INDEX(SUBSTRING(empDetails,LOCATE('phone:',empDetails)),',',1),'phone:','') as phone
FROM Table1 where empDetails REGEXP '[[:<:]]phone[[:>:]]'; 

- -输出 - -

PHONE
7845982563
7889451524
7889451524

使用工作示例检查更新sql 小提琴:

希望这会有所帮助!

于 2013-07-24T13:21:25.560 回答
0

尝试

update  employee set 
phone = SUBSTR(SUBSTRING_INDEX(empDetails,'phone:',-1) from 1 for  instr(SUBSTRING_INDEX(empDetails,'phone:',-1),',')-1)
where employee.empDetails LIKE '%phone:%'

请参阅http://sqlfiddle.com/#!2/10c8c4/40

于 2013-07-24T13:26:37.857 回答