特定字元後分割列的 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)
廣告