如何删除mysql表中的数字字符?

时间:2016-04-08 06:01:00

标签: mysql

我在MySQL中有一个名为“Actress”的表 我想从“name”列中删除所有数字字符

select * from Actress  limit 5;
+-------+---------------------+
| code  | name                |
+-------+---------------------+
| 11455 | Hanshika_Motwani_19 |
| 11457 | Kajal_Agrwal_11     |
| 11458 | Ileana_21           |
| 11459 | Kaveri_Jha_11       |
| 11462 | Kaveri_Jha_18       |
+-------+---------------------+
5 rows in set (0.00 sec)

如何更新我的表以删除MySQL表中的数字字符,以便我可以得到如下结果

select * from Actress  limit 5;
+-------+---------------------+
| code  | name                |
+-------+---------------------+
| 11455 | Hanshika_Motwani_   |
| 11457 | Kajal_Agrwal_       |
| 11458 | Ileana_21           |
| 11459 | Kaveri_Jha_         |
| 11462 | Kaveri_Jha_         |
+-------+---------------------+

3 个答案:

答案 0 :(得分:3)

  

嘿,我得到了一个答案,我已经执行了以下查询,这解决了我的问题   从mysql表中删除所有数字的问题

update Actress SET name  = REPLACE(name, '1', '');
update Actress SET name  = REPLACE(name, '2', '');
update Actress SET name  = REPLACE(name, '3', '');
update Actress SET name  = REPLACE(name, '4', '');
update Actress SET name  = REPLACE(name, '5', '');
update Actress SET name  = REPLACE(name, '6', '');
update Actress SET name  = REPLACE(name, '7', '');
update Actress SET name  = REPLACE(name, '8', '');
update Actress SET name  = REPLACE(name, '9', '');
update Actress SET name  = REPLACE(name, '0', '');

答案 1 :(得分:1)

看起来不太好,但它确实有效。它从字符串中删除任何数字

SELECT
    REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE (
    REPLACE( REPLACE( REPLACE( REPLACE('Hallo_1234567890_99','0','')
    ,'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9','');


update Actress 
SET name  = REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE (
        REPLACE( REPLACE( REPLACE( REPLACE(name,'0','')
        ,'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9','');

如果您使用MariaDB,则可以使用REGEX_REPLACE:

update Actress
 set name =  REGEXP_REPLACE(name,'[0-9]','');

<强>示例

MariaDB [(none)]> SELECT REGEXP_REPLACE('A1B2C44','[0-9]','');
+--------------------------------------+
| REGEXP_REPLACE('A1B2C44','[0-9]','') |
+--------------------------------------+
| ABC                                  |
+--------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

答案 2 :(得分:0)

在运行update命令之前尝试SELECT。内部LOCATE函数首次出现&#39; &#39;外部Locate为您提供第二次出现。 LEFT为你提供所有角色,直到第二次出现&#39; &#39;。我没有试过这个,所以希望它有效。

SELECT LEFT(name,LOCATE('_',name,LOCATE('_',name)+1)) as name FROM Actress WHERE 1


UPDATE Actress SET name=LEFT(name,LOCATE('_',name,LOCATE('_',name)+1)) WHERE 1