Using MySQL SELECT for simple BOOLEAN evaluation?


You can use CASE statement for this. Let us see an example −

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 some records in the table using insert command. The query is as follows −

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)

Display all records from the table using select statement. The query is as follows −

mysql> select *from BooleanEvaluationDemo;

Here is the output −

+----+------------+-------------+
| Id | FirstValue | SecondValue |
+----+------------+-------------+
| 1  | 10         | 5           |
| 2  | 15         | 20          |
| 3  | 50         | 40          |
| 4  | 500        | 1000        |
+----+------------+-------------+
4 rows in set (0.00 sec)

The following is the query to SELECT for simple BOOLEAN evaluation −

mysql> SELECT FirstValue,SecondValue,CASE WHEN FirstValue > SecondValue THEN
'true' ELSE 'false' END AS FirstValuesGreaterThanSecond from BooleanEvaluationDemo;

Here is the output −

+------------+-------------+-------------------------------+
| FirstValue | SecondValue | FirstValuesGreaterThanSecond  |
+------------+-------------+-------------------------------+
| 10         | 5           | true                          |
| 15         | 20          | false                         |
| 50         | 40          | true                          |
| 500        | 1000        | false                         |
+------------+-------------+-------------------------------+
4 rows in set (0.00 sec)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements