MySQL 查询将所有记录更新为只有首字母大写,其他字母小写
首先让我们创建一个表 −
mysql> create table DemoTable2017 -> ( -> Name text -> ); Query OK, 0 rows affected (0.55 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable2017 values('JOHN SMITH,MYSQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2017 values('DAVID MILLER,MONGODB'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2017 values('CHRIS BROWN,JAVA'); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable2017;
这将生成以下输出 −
+----------------------+ | Name | +----------------------+ | JOHN SMITH,MYSQL | | DAVID MILLER,MONGODB | | CHRIS BROWN,JAVA | +----------------------+ 3 rows in set (0.00 sec)
以下是仅将首字母设为大写,其余设为小写的查询 −
mysql> update DemoTable2017 -> set Name=REPLACE(CONCAT(UPPER(LEFT(Name, 1)), LOWER(SUBSTRING(Name, 2))),' , ', ', '); Query OK, 3 rows affected (0.16 sec) Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录 −
mysql> select *from DemoTable2017;
这将生成以下输出 −
+----------------------+ | Name | +----------------------+ | John smith,mysql | | David miller,mongodb | | Chris brown,java | +----------------------+ 3 rows in set (0.00 sec)
广告