要更新布尔值,可以使用SET。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, isMarried boolean );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(isMarried) values(false); mysql> insert into DemoTable(isMarried) values(true); mysql> insert into DemoTable(isMarried) values(true); mysql> insert into DemoTable(isMarried) values(false);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-----------+ | Id | isMarried | +----+-----------+ | 1 | 0 | | 2 | 1 | | 3 | 1 | | 4 | 0 | +----+-----------+ 4 rows in set (0.00 sec)
以下是更新布尔值的查询-
mysql> update DemoTable set isMarried = !isMarried where Id=4; Rows matched: 1 Changed: 1 Warnings: 0
让我们再次显示表记录-
mysql> select *from DemoTable;
这将产生以下输出。在这里,我们更新了ID 4的布尔值-
+----+-----------+ | Id | isMarried | +----+-----------+ | 1 | 0 | | 2 | 1 | | 3 | 1 | | 4 | 1 | +----+-----------+ 4 rows in set (0.00 sec)
查看上面的示例输出,具有ID 4的行已更新。