特定字符后分割列的 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)
广告