
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL Query with Two Boolean Conditions to Extract Date Based on Hour
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;
Output
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;
Output
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