如何使用 MySQL 将整个字符串变为小写,同时保持首字母大写?\\
mysqlmysqli database
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (1.32 sec)
使用 insert 命令在表中插入一些记录 −
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)