如何在 MySQL 中分割一個數字查詢結果?


要分割一個數字查詢結果,你可以在 MySQL 中使用 CONCAT() 函式。我們首先建立一個表 −

mysql> create table DemoTable
   (
   StudentId int
   );
Query OK, 0 rows affected (0.68 sec)

現在你可以使用 insert 命令向表中插入一些記錄 −

mysql> insert into DemoTable values(2222);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(5555);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(4567);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(8905);
Query OK, 1 row affected (0.15 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

+-----------+
| StudentId |
+-----------+
| 2222      | 
| 5555      |
| 4567      |
| 8905      |
+-----------+
4 rows in set (0.00 sec)

以下是分割數字查詢結果的查詢。此處,我們分割了值的第一個數字 −

mysql> select concat(left(StudentId, 1), '/',right(StudentId, length(StudentId)-1)) splitNumericalValue from DemoTable;

輸出

+---------------------+
| splitNumericalValue |
+---------------------+
| 2/222               |
| 5/555               |
| 4/567               |
| 8/905               |
+---------------------+
4 rows in set (0.00 sec)

更新於: 30-Jul-2019

115 瀏覽量

開啟你的 職業生涯

完成課程即可獲得認證資格

立即開始
廣告
© . All rights reserved.