如何在一次调用中更改 MySQL 表中每个字段的大小写?

mysqlmysqli database

您可以将 update 与 lower() 函数一起使用来实现此目的。我们首先创建一个表。 −

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)

相关文章