MySQL 选择以数字开头的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4511945/
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 18:05:18  来源:igfitidea点击:

Select values that begin with a number

sqlmysqlselectnumbers

提问by Omega

I have a table with a column containing data that begin with numbers too, on MySQL

我在 MySQL 上有一个表,其中包含一列也以数字开头的数据

How can I select the rows that begin only with a number?

如何选择仅以数字开头的行?

回答by Sarfraz

SELECT * FROM YourTable WHERE YourColumn regexp '^[0-9]+'

回答by codaddict

You can do:

你可以做:

SELECT *
FROM MyTable
WHERE MyColumn REGEXP '^[0-9]';

The regular expression used is ^[0-9].

使用的正则表达式是^[0-9].

^    - Start anchor, used to ensure the pattern matches start of the string.
[    - Start of character class.
0-9  - Any digit
]    - End of character class

Effectively we are trying to select those values in the column that begin with a digit.

实际上,我们正在尝试选择列中以 digit 开头的那些值。

Demo:

演示:

mysql> select * from tab;
+-------+
| col   |
+-------+
| 1foo  |
| foo   |
| 10foo |
| foo10 |
+-------+
4 rows in set (0.00 sec)

mysql> select * from tab where col regexp '^[0-9]';
+-------+
| col   |
+-------+
| 1foo  |
| 10foo |
+-------+
2 rows in set (0.00 sec)

回答by ypercube??

Yet another way:

还有一种方式:

WHERE LEFT(columnName,1) IN ('0','1','2','3','4','5','6','7','8','9')

and with common charsets and collations, this would work and use an index on the column:

并且使用通用字符集和排序规则,这将起作用并在列上使用索引:

WHERE columnName >= '0' AND columnName < ':'

回答by Lukas Greso

also

SELECT * FROM YourTable
WHERE YourColumn LIKE '[0-9]%';

回答by Vaibhav Kumar

SELECT * FROM TABLE T
WHERE T.COLUMNNAME REGEXP '^[0-9]';

Another answer is:

另一个答案是:

SELECT * FROM TABLE T
WHERE T.COLUMNNAME RLIKE '^[0-9]';