從 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