简体   繁体   中英

date between two dates in two fields

I am working in hotel domain. i was created the inventory page for each hotel along with each rooms.

In data structure of db i have created with start date and end date. while inventory we pass the record in between dates.

SELECT `crs_room_type_detail`.*, `crs_room_type`.`room_type_name`
FROM (`crs_room_type_detail`)
JOIN `crs_room_type` ON `crs_room_type_detail`.`room_type_id` = `crs_room_type`.`room_type_id`
WHERE `crs_room_type_detail`.`hotel_id` =  '4'
AND `crs_room_type_detail`.`sdate` >=  '2016-09-21'
AND `crs_room_type_detail`.`edate` <=  '2016-10-04'
GROUP BY `crs_room_type_detail`.`room_type_id`

This the query to get the result. for Rooms we had the sdate : 2016-09-20 and edate : 2016-09-30 this criteria it wont work, suppose i changes condition like sdate <= '2016-09-21' and edate >= '2016-10-04' its wont work some other criteria

Try out the following script:

SELECT `crs_room_type_detail`.*, `crs_room_type`.`room_type_name`, date
FROM (`crs_room_type_detail`)
JOIN `crs_room_type` ON `crs_room_type_detail`.`room_type_id` =
`crs_room_type`.`room_type_id`
WHERE `crs_room_type_detail`.`hotel_id` =  '4'
AND date(`crs_room_type_detail`.`sdate`) >=  date('2016-09-21')
AND date(`crs_room_type_detail`.`edate`) <=  date('2016-10-04')
GROUP BY `crs_room_type_detail`.`room_type_id`

use between to get date range:

SELECT `crs_room_type_detail`.*, `crs_room_type`.`room_type_name`
FROM (`crs_room_type_detail`)
JOIN `crs_room_type` ON `crs_room_type_detail`.`room_type_id` = `crs_room_type`.`room_type_id`
WHERE `crs_room_type_detail`.`hotel_id` =  '4'
AND `crs_room_type_detail`.`sdate` between  '2016-09-21' and '2016-10-04'
GROUP BY `crs_room_type_detail`.`room_type_id`

Try This One

 SELECT `crs_room_type_detail`.*, `crs_room_type`.`room_type_name`
    FROM (`crs_room_type_detail`)
    JOIN `crs_room_type` ON `crs_room_type_detail`.`room_type_id` = `crs_room_type`.`room_type_id`
    WHERE `crs_room_type_detail`.`hotel_id` =  '4'
    AND `crs_room_type_detail`.`sdate` BETWEEN '2016-09-21' AND '2016-10-04'
    AND `crs_room_type_detail`.`edate` BETWEEN '2016-09-21' AND '2016-10-04'
    GROUP BY `crs_room_type_detail`.`room_type_id`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM