返回含有正值行的 MySQL 查询为 TRUE 吗?
要针对正值返回 TRUE,针对负值返回 FALSE,请使用 MySQL IF()。我们首先创建一个表格 -
mysql> create table DemoTable2038 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value int -> ); Query OK, 0 rows affected (0.87 sec)
使用 insert 命令向表格中插入一些记录 -
mysql> insert into DemoTable2038(Value) values(57); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2038(Value) values(-100);; Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2038(Value) values(-78); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable2038(Value) values(78); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2038(Value) values(91); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable2038(Value) values(-34); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表格中的所有记录 -
mysql> select *from DemoTable2038;
这将产生以下输出 -
+----+-------+ | Id | Value | +----+-------+ | 1 | 57 | | 2 | -100 | | 3 | -78 | | 4 | 78 | | 5 | 91 | | 6 | -34 | +----+-------+ 6 rows in set (0.00 sec)
以下是对具有正值的行的返回 true 的查询 -
mysql> select *,if(Value > 0,true,false) as Result from DemoTable2038;
这将产生以下输出 -
+----+-------+--------+ | Id | Value | Result | +----+-------+--------+ | 1 | 57 | 1 | | 2 | -100 | 0 | | 3 | -78 | 0 | | 4 | 78 | 1 | | 5 | 91 | 1 | | 6 | -34 | 0 | +----+-------+--------+ 6 rows in set (0.00 sec)
广告