如何将 MySQL 表中的所有记录从大写转换为小写?


使用 UPDATE 命令及 LOWER() 方法将 MySQL 表中所有记录从大写转换为小写。

我们首先创建一个表 -

mysql> create table DemoTable
   (
   Id varchar(100),
   StudentFirstName varchar(20),
   StudentLastName varchar(20),
   StudentCountryName varchar(10)
   );
Query OK, 0 rows affected (0.61 sec)

使用 insert 命令插入一些记录到表中 -

mysql> insert into DemoTable values('STU-101','John','Smith','US');
Query OK, 1 row affected (0.59 sec)
mysql> insert into DemoTable values('STU-102','John','Doe','UK');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('STU-103','David','Miller','AUS');
Query OK, 1 row affected (0.19 sec)

使用 select 语句显示表中所有记录的查询如下 -

mysql> select *from DemoTable;

这将生成以下输出 -

+---------+------------------+-----------------+--------------------+
| Id      | StudentFirstName | StudentLastName | StudentCountryName |
+---------+------------------+-----------------+--------------------+
| STU-101 | John             | Smith           | US                 |
| STU-102 | John             | Doe             | UK                 |
| STU-103 | David            | Miller          | AUS                |
+---------+------------------+-----------------+--------------------+
3 rows in set (0.00 sec)

下面是用于在一个 MySQL 表中的每一个字段上更改大小写的查询,只需调用一次即可。

mysql> update DemoTable
set Id=lower(Id),
StudentFirstName=lower(StudentFirstName),
StudentLastName=lower(StudentLastName),
StudentCountryName=lower(StudentCountryName);
Query OK, 3 rows affected (0.22 sec)
Rows matched: 3 Changed: 3 Warnings: 0

使用 select 语句显示表中所有记录以检查上述查询所做的更改 -

mysql> select *from DemoTable;

这将生成以下输出 -

+---------+------------------+-----------------+--------------------+
| Id      | StudentFirstName | StudentLastName | StudentCountryName |
+---------+------------------+-----------------+--------------------+
| stu-101 | john             | smith           | us                 |
| stu-102 | john             | doe             | uk                 |
| stu-103 | david            | miller          | aus                |
+---------+------------------+-----------------+--------------------+
3 rows in set (0.00 sec)

更新于: 30-7 月 -2019

717 次浏览

开启您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.