如何在 MySQL 中使整個字串小寫,同時保持首字母大寫?


我們先建立一個表——

mysql> create table DemoTable
   -> (
   -> Name varchar(100)
   -> );
Query OK, 0 rows affected (1.32 sec)

使用插入命令在表中插入一些記錄——

mysql> insert into DemoTable values('JOhn');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values('CHRIS');
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable values('DAVID');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable values('RObert');
Query OK, 1 row affected (0.21 sec)

使用 select 語句顯示錶中的所有記錄——

mysql> select *from DemoTable;

輸出

+--------+
| Name   |
+--------+
| JOhn   |
| CHRIS  |
| DAVID  |
| RObert |
+--------+
4 rows in set (0.00 sec)

以下是在 MySQL 中將整個字串小寫,同時保持首字母大寫的查詢——

mysql> update DemoTable
   -> set Name= CONCAT(UCASE(LEFT(Name, 1)), LCASE(SUBSTRING(Name, 2)));
Query OK, 4 rows affected (0.30 sec)
Rows matched: 4 Changed: 4 Warnings: 0

讓我們再次檢查表中的所有記錄。

mysql> select *from DemoTable;

輸出

+--------+
| Name   |
+--------+
| John   |
| Chris  |
| David  |
| Robert |
+--------+
4 rows in set (0.00 sec)

更新於: 2019 年 7 月 30 日

100 次瀏覽

開啟您的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.