如何在MySQL中更新布尔值?
您可以使用UPDATE命令更新布尔值。如果使用BOOLEAN数据类型,则MySQL在内部将其转换为tinyint(1)。它可以采用true或false文字,其中true表示tinyint(1)的值为1,false表示tinyint(1)的值为0。
语法如下-
UPDATE yourTableName SET yourColumnName = yourValue WHERE yourCondition;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table UpdateBooleans -> ( -> Id int NOT NULL AUTO_INCREMENT, -> isSuccessful BOOLEAN, -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into UpdateBooleans(isSuccessful) values(true); mysql> insert into UpdateBooleans(isSuccessful) values(false); mysql> insert into UpdateBooleans(isSuccessful) values(true); mysql> insert into UpdateBooleans(isSuccessful) values(false); mysql> insert into UpdateBooleans(isSuccessful) values(false); mysql> insert into UpdateBooleans(isSuccessful) values(false); mysql> insert into UpdateBooleans(isSuccessful) values(true);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from UpdateBooleans;
以下是输出-
+----+--------------+ | Id | isSuccessful | +----+--------------+ | 1 | 1 | | 2 | 0 | | 3 | 1 | | 4 | 0 | | 5 | 0 | | 6 | 0 | | 7 | 1 | +----+--------------+ 7 rows in set (0.00 sec)
这是更新布尔值的查询。让我们将所有0更新为1:
mysql> update UpdateBooleans set isSuccessful = true where isSuccessful = false; Rows matched: 4 Changed: 4 Warnings: 0
再次显示表中的记录。查询如下:
mysql> select *from UpdateBooleans;
以下是输出:
+----+--------------+ | Id | isSuccessful | +----+--------------+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 1 | | 5 | 1 | | 6 | 1 | | 7 | 1 | +----+--------------+ 7 rows in set (0.00 sec)
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短