使用 MySQL SELECT 进行简单的 BOOLEAN 评估?
您可以为此使用 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)
以下是用于简单 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)
广告