我有一张这样的表
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`tel_1` text,
`tel_2` text,
`tel_3` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+----+------------------------------------+-----------------------------------------------+-------+
| id | tel_1 | tel_2 | tel_3 |
+----+------------------------------------+-----------------------------------------------+-------+
| 1 | 123-412-3455 | 1276ー364739−181 | NULL |
| 2 | 714-212-3839 Not Using | No Info | NULL |
| 3 | 12+13E | NULL | NULL |
| 4 | 0123ー3432-1233 Ext : 602 | NULL | NULL |
+----+------------------------------------+-----------------------------------------------+-------+
我想清理下面的数据:
+----+--------------+------------+---------------+------------+-------+------------+
| id | tel_1 | tel_1_desc | tel_2 | tel_2_desc | tel_3 | tel_3_desc |
+----+--------------+------------+---------------+------------+-------+------------+
| 1 | 1234123455 | NULL | 1276364739181 | NULL | NULL | NULL |
| 2 | 7142123839 | Not Using | NULL | No Info | NULL | NULL |
| 3 | 12+13E | NULL | NULL | NULL | NULL | NULL |
| 4 | 012334321233 | Ext : 602 | NULL | NULL | NULL | NULL |
+----+--------------+------------+---------------+------------+-------+------------+
以下是我需要的清单:
我设法为MySQL创建#3的存储函数,但是我不能为每个记录循环并更新......
CREATE FUNCTION `multibyte2cv`(`str` TEXT) RETURNS text CHARSET utf8
BEGIN
DECLARE int_len INT(2);
DECLARE int_z VARCHAR(10) DEFAULT '1234567890';
DECLARE int_h VARCHAR(10) DEFAULT '1234567890';
SET int_len = CHAR_LENGTH(int_z);
WHILE int_len > 0 DO
SET str = REPLACE(str, SUBSTRING(int_z,int_len,1), SUBSTRING(int_h,int_len,1));
SET int_len = int_len - 1;
END WHILE;
RETURN str;
END;
我对数据库领域很陌生,并试图找到一些我可以解决这个问题的东西......
答案 0 :(得分:1)
在UPDATE
查询中调用该函数:
UPDATE test
SET tel_1 = multibyte2cv(tel_1), tel_2 = multibyte2cv(tel_2), tel_3 = multibyte2cv(tel_3)