在 MySQL 中,在连字符后拆分列并显示剩余值?

mysqlmysqli database更新于 2025/9/5 2:20:17

要在连字符后拆分列,请使用 SUBSTRING_INDEX() 方法 −

select substring_index(yourColumnName,'-',-1) AS anyAliasName from yourTableName;

首先我们创建一个表 −

mysql> create table DemoTable
   -> (
   -> StreetName text
   -> );
Query OK, 0 rows affected (0.60 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable values('Paris Hill St.-CA-83745646') ;
Query OK, 1 row affected (0.32 sec)

mysql> insert into DemoTable values('502 South Armstrong Street-9948443');
Query OK, 1 row affected (0.20 sec)

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

mysql> select *from DemoTable;

这将产生以下输出 −

+------------------------------------+
| StreetName                         |
+------------------------------------+
| Paris Hill St.-CA-83745646         |
| 502 South Armstrong Street-9948443 |
+------------------------------------+
2 rows in set (0.00 sec)

以下是在特定字符后拆分列的查询 −

mysql> select substring_index(StreetName,'-',-1) AS Split from DemoTable;

这将产生以下输出 −

+----------+
| Split    |
+----------+
| 83745646 |
| 9948443  |
+----------+
2 rows in set (0.00 sec)

相关文章


有用资源