Tutorialspoint

MySQL query with two boolean conditions to extract date based on hour?


1 Answer
Sharon Christine

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> AdmissionDate datetime
   -> );
Query OK, 0 rows affected (0.75 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-10 10:45:10');
Query OK, 1 row affected (0.30 sec)

mysql> insert into DemoTable values('2019-02-12 20:50:00');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('2019-02-12 16:10:19');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+
| AdmissionDate       |
+---------------------+
| 2019-01-10 10:45:10 |
| 2019-02-12 20:50:00 |
| 2019-02-12 16:10:19 |
+---------------------+
3 rows in set (0.00 sec)

Here is the query to with two boolean conditions takes way longer than querying each condition separately −

mysql> select *from DemoTable where EXTRACT(HOUR from AdmissionDate) not between 8 and 10;

This will produce the following output −

+---------------------+
| AdmissionDate       |
+---------------------+
| 2019-02-12 20:50:00 |
| 2019-02-12 16:10:19 |
+---------------------+
2 rows in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.