MySQL 如何拆分数字查询结果
要拆分数字查询结果,可以使用MySQL中的CONCAT()函数。首先创建一个表−
mysql> create table DemoTable
(
StudentId int
);
Query OK, 0 rows affected (0.68 sec)
现在可以使用插入命令将一些记录插入表中−
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;
阅读更多:MySQL 教程
输出
+-----------+
| 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)