在 MySQL 中更新一列並移除尾部下劃線值
要移除尾部值,請使用 TRIM() 函式,如下面的更新語法所示 -
update yourTableName set yourColumnName=trim(trailing '_' from yourColumnName);
讓我們先建立一個表 -
mysql> create table DemoTable1521 -> ( -> StudentCode varchar(20) -> ); Query OK, 0 rows affected (1.33 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable1521 values('345_'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1521 values('12345'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1521 values('9084_'); Query OK, 1 row affected (1.29 sec)
使用 select 語句顯示錶中的所有記錄 -
mysql> select * from DemoTable1521;
這將產生以下輸出 -
+-------------+ | StudentCode | +-------------+ | 345_ | | 12345 | | 9084_ | +-------------+ 3 rows in set (0.00 sec)
以下是更新 MySQL 中的列並進行裁剪的查詢 -
mysql> update DemoTable1521 -> set StudentCode=trim(trailing '_' from StudentCode); Query OK, 2 rows affected (0.34 sec) Rows matched: 3 Changed: 2 Warnings: 0
讓我們再次檢查表記錄 -
mysql> select * from DemoTable1521;
這將產生以下輸出 -
+-------------+ | StudentCode | +-------------+ | 345 | | 12345 | | 9084 | +-------------+ 3 rows in set (0.00 sec)
廣告