从 MySQL 记录中获取数字?
使用 CONVERT() 函数或正则表达式。CONVERT() 方法将值从一种数据类型转换为另一种数据类型。这最终将为我们获取数字。让我们看一个示例。
首先,我们将创建一个表。
mysql> create table textIntoNumberDemo -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.47 sec)
插入一些记录。
mysql> insert into textIntoNumberDemo values('John-11');
Query OK, 1 row affected (0.11 sec)
mysql> insert into textIntoNumberDemo values('John-12');
Query OK, 1 row affected (0.17 sec)
mysql> insert into textIntoNumberDemo values('John-2');
Query OK, 1 row affected (0.11 sec)
mysql> insert into textIntoNumberDemo values('John-4');
Query OK, 1 row affected (0.14 sec)
显示所有记录。
mysql> select *from textIntoNumberDemo;
以下是输出。
+---------+ | Name | +---------+ | John-11 | | John-12 | | John-2 | | John-4 | +---------+ 4 rows in set (0.00 sec)
提取数字的语法。
SELECT yourColumnName,CONVERT(SUBSTRING_INDEX(yourColumnName,'-',-1),UNSIGNED INTEGER) AS yourVariableName FROM yourTableName order by yourVariableName;
以下为查询。
mysql> SELECT Name,CONVERT(SUBSTRING_INDEX(Name,'-',-1),UNSIGNED INTEGER) AS MyNumber -> FROM textIntoNumberDemo -> order by MyNumber;
以下是输出。
+---------+----------+ | Name | MyNumber | +---------+----------+ | John-2 | 2 | | John-4 | 4 | | John-11 | 11 | | John-12 | 12 | +---------+----------+ 4 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP