检查字符串是否包含数字。

时间:2022-09-08 01:44:08

How do I check if a string in a MySQL field contains a number then delete them?

如何检查MySQL字段中的字符串是否包含数字,然后删除它们?

Example table:

示例表:

tbl_tags
 -------------
| id | tag    |
 -------------
| 1  | hello  |
| 2  | hello2 |
| 3  | 2hello |
| 4  | hel3lo |
 -------------

The only way I can think of is to do each number individually and use LIKE '%1%' OR LIKE '%2%' etc. But there must be an easier way?

我能想到的唯一的方法是分别使用每个数字,比如“%1%”或“%2%”等等。但一定有更简单的方法吗?

2 个解决方案

#1


43  

Check out the MySQL Regex methods. Something like the following should work.

查看MySQL Regex方法。下面这样的内容应该可以。

SELECT * FROM table WHERE tag REGEXP '[0-9]'

#2


4  

SELECT * 
FROM clients
WHERE name REGEXP '^[[:digit:]]+$'
ORDER BY `clients`.`country_id` DESC 
LIMIT 0 , 30

this is the answer if you're looking for strings with only digits

这就是答案,如果你在寻找只有数字的字符串。

#1


43  

Check out the MySQL Regex methods. Something like the following should work.

查看MySQL Regex方法。下面这样的内容应该可以。

SELECT * FROM table WHERE tag REGEXP '[0-9]'

#2


4  

SELECT * 
FROM clients
WHERE name REGEXP '^[[:digit:]]+$'
ORDER BY `clients`.`country_id` DESC 
LIMIT 0 , 30

this is the answer if you're looking for strings with only digits

这就是答案,如果你在寻找只有数字的字符串。