要为正值返回TRUE,为负返回FALSE,请使用MySQL IF()。让我们首先创建一个表-
create table DemoTable2038 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Value int -> );
使用插入命令在表中插入一些记录-
insert into DemoTable2038(Value) values(57); insert into DemoTable2038(Value) values(-100);; insert into DemoTable2038(Value) values(-78); insert into DemoTable2038(Value) values(78); insert into DemoTable2038(Value) values(91); insert into DemoTable2038(Value) values(-34);
使用select语句显示表中的所有记录-
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的查询-
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)