Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql to check room is exists for a particular date with time duration

Tags:

sql

mysql

I have check room is exists between two time for particular date.

MY TABLE LIKE THIS

I have try following two query its run some time rights but, when i select 10:00 AM to 12:00 PM at time wrong results means not return any records.

QUERY-1 :

SELECT 1 FROM `timetable_details` WHERE (
        ((`td_from` <= '10:00:00') AND (`td_to` > '10:00:00')) 
        OR 
        ((`td_from` < '12:20:00') AND (`td_to` >= '12:20:00'))

    ) AND ((`td_room`='1') AND (`td_date`='2016-01-25'))

QUERY-2 :

SELECT 1 FROM `timetable_details` WHERE (
            (`td_from` > '07:00:00') AND (`td_to` < '08:00:00')
      ) AND ((`td_room`='1') AND (`td_date`='2016-01-25'))

I have get td_id = 4 number row but is not returns.

like image 369
Hiren Bhut Avatar asked Jan 11 '16 09:01

Hiren Bhut


People also ask

How can I get specific date records in MySQL?

You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.

Which function displays current date and time in MySQL?

MySQL NOW() Function The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.

How do you display time and date in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format.

How many ways we can we find the current date using MySQL?

Solution: We'll use one of two functions, CURRENT_TIMESTAMP or NOW(), to get the current date and time.


1 Answers

You can use between with OR condition for both columns as below :

SELECT 1 FROM `timetable_details` WHERE (((((`td_from` BETWEEN '10:00:00' AND '12:30:00') OR (`td_to` BETWEEN '10:00:00' AND '12:30:00')) AND ((`td_room`='1') AND (`td_date`='2016-01-25') AND (`td_status` IS NULL))) AND (`td_from` <> '12:30:00')) AND (`td_to` <> '10:00:00'))
like image 86
Karmraj Zala Avatar answered Oct 16 '22 07:10

Karmraj Zala