使用 MySQL SELECT 进行简单的 BOOLEAN 评估?
mysqlmysqli database
您可以使用 CASE 语句进行此操作。让我们看一个例子 −
mysql> create table BooleanEvaluationDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstValue int, -> SecondValue int -> ); Query OK, 0 rows affected (0.71 sec)
使用 insert 命令在表中插入一些记录。 查询语句如下 −
mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(10,5); Query OK, 1 row affected (0.20 sec) mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(15,20); Query OK, 1 row affected (0.16 sec) mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(50,40); Query OK, 1 row affected (0.14 sec) mysql> insert into BooleanEvaluationDemo(FirstValue,SecondValue) values(500,1000); Query OK, 1 row affected (0.13 sec)
使用 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)
以下是用于简单布尔评估的 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)