您可以为此使用CASE语句。让我们看一个例子-
mysql> create table BooleanEvaluationDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstValue int, -> SecondValue int -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(10,5); mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(15,20); mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(50,40); mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(500,1000);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from BooleanEvaluationDemo;
这是输出-
+----+------------+-------------+ | Id | FirstValue | SecondValue | +----+------------+-------------+ | 1 | 10 | 5 | | 2 | 15 | 20 | | 3 | 50 | 40 | | 4 | 500 | 1000 | +----+------------+-------------+ 4 rows in set (0.00 sec)
以下是用于简单BOOLEAN评估的SELECT查询-
mysql> SELECT FirstValue,SecondValue,CASE WHEN FirstValue > SecondValue THEN 'true' ELSE 'false' END AS FirstValuesGreaterThanSecond from BooleanEvaluationDemo;
这是输出-
+------------+-------------+-------------------------------+ | FirstValue | SecondValue | FirstValuesGreaterThanSecond | +------------+-------------+-------------------------------+ | 10 | 5 | true | | 15 | 20 | false | | 50 | 40 | true | | 500 | 1000 | false | +------------+-------------+-------------------------------+ 4 rows in set (0.00 sec)