MySQL 如何使用SUBSTRING_INDEX拆分字符串

MySQL 如何使用SUBSTRING_INDEX拆分字符串

让我们首先创建一个表 –

mysql> create table DemoTable1465
   -> (
   -> Name varchar(40)
   -> );
Query OK, 0 rows affected (0.54 sec)

使用插入命令向表中插入一些记录 –

mysql> insert into DemoTable1465 values('Chris Brown');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1465 values('David Miller');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable1465 values('John Doe');
Query OK, 1 row affected (0.14 sec)

使用 select 语句显示表中的所有记录 –

mysql> select * from DemoTable1465;

这将产生以下输出 –

+--------------+
| Name         |
+--------------+
| Chris Brown  |
| David Miller |
| John Doe     |
+--------------+
3 rows in set (0.00 sec)

以下是使用SUBSTRING_INDEX()拆分MySQL中字符串的查询。这里,我们正在拆分名字和姓氏字符串 –

mysql> select
   -> substring_index(substring_index(Name, ' ', 1), ' ', -1) as StudentFirstName,
   ->   substring_index(substring_index(Name, ' ', 3), ' ', -1) AS StudentLastName
   -> from DemoTable1465;

这将产生以下输出 –

+------------------+-----------------+
| StudentFirstName | StudentLastName |
+------------------+-----------------+
| Chris            |           Brown |
| David            |          Miller |
| John             |             Doe |
+------------------+-----------------+
3 rows in set (0.00 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程