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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3269857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:34:33  来源:igfitidea点击:

Check if a string contains numbers

mysqlstringnumbers

提问by pjau

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?

我能想到的唯一方法是单独执行每个数字并使用LIKE '%1%' OR LIKE '%2%'等。但必须有更简单的方法吗?

回答by Jason McCreary

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

查看MySQL Regex 方法。像下面这样的东西应该可以工作。

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

回答by user3200518

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

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