在 MySQL 中以连字符分割一列并显示剩余值?
要以连字符分割列,请使用 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)
使用插入命令在表中插入一些记录 −
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)
广告