要更新单个列,请按照以下语法使用UPDATE和SET-
update yourTableName set yourColumnName=yourValue;
让我们首先创建一个表-
mysql> create table DemoTable1873 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1873(FirstName) values('John'); mysql> insert into DemoTable1873(FirstName) values('Adam'); mysql> insert into DemoTable1873(FirstName) values('David'); mysql> insert into DemoTable1873(FirstName) values('Sam');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1873 ;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Adam | | 3 | David | | 4 | Sam | +----+-----------+ 4 rows in set (0.00 sec)
这是更新MySQL表中单个列的查询-
mysql> update DemoTable1873 set FirstName='Robert'; Rows matched: 4 Changed: 4 Warnings: 0
让我们再次检查表记录-
mysql> select * from DemoTable1873 ;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | Robert | | 2 | Robert | | 3 | Robert | | 4 | Robert | +----+-----------+ 4 rows in set (0.00 sec)