解析字串以從由下劃線分隔的大字串中獲取數字


讓我們首先建立一個數據表

mysql> create table DemoTable1961
   (
   Title text
   );
Query OK, 0 rows affected (0.00 sec)

使用 insert 命令向資料表中插入一些記錄

mysql> insert into DemoTable1961 values('You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts');
Query OK, 1 row affected (0.00 sec)

使用 select 語句從資料表中顯示所有記錄

mysql> select * from DemoTable1961;

這會生成以下輸出

+------------------------------------------------------------------------------------+
| Title                                                                              |
+------------------------------------------------------------------------------------+
| You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts |
+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

以下是對 MySQLi 查詢以實現 CAST() 並從字串中解析出數字

mysql> select cast(replace(replace('You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts','You_can_remove_the_string_part_only-',''),
   '-But_You_can_not_remove_the_numeric_parts','') as unsigned) as Output from DemoTable1961;

這會生成以下輸出

+--------+
| Output |
+--------+
|  10001 |
+--------+
1 row in set (0.00 sec)

更新於: 2019-12-31

85 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.