使用单一查询将 MySQL 表的所有列设置为特定值
我们首先创建一个表 −
mysql> create table DemoTable ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(40), ClientAge int, ClientCountryName varchar(40) ); Query OK, 0 rows affected (0.57 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('Chris',25,'US'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('Bob',55,'UK'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ClientName,ClientAge,ClientCountryName) values('David',45,'AUS'); Query OK, 1 row affected (0.14 sec)
使用 select 语句在表中显示所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+----------+------------+-----------+-------------------+ | ClientId | ClientName | ClientAge | ClientCountryName | +----------+------------+-----------+-------------------+ | 1 | Chris | 25 | US | | 2 | Bob | 55 | UK | | 3 | David | 45 | AUS | +----------+------------+-----------+-------------------+ 3 rows in set (0.00 sec)
以下是将 MySQL 表的所有列设置为特定值的一个查询——
mysql> update DemoTable set ClientName='Sam',ClientAge=48,ClientCountryName='AUS' where ClientId=2; Query OK, 1 row affected (0.22 sec) Rows matched : 1 Changed : 1 Warnings : 0
让我们再次检查表记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+----------+------------+-----------+-------------------+ | ClientId | ClientName | ClientAge | ClientCountryName | +----------+------------+-----------+-------------------+ | 1 | Chris | 25 | US | | 2 | Sam | 48 | AUS | | 3 | David | 45 | AUS | +----------+------------+-----------+-------------------+ 3 rows in set (0.00 sec)
广告