Tuesday, January 19, 2010

Comparing Dates

Based from Professional PHP Developers http://groups.google.com/group/professional-php/browse_thread/thread/3472388d5af249db post question.

This is the sample season_dates table:



This is the sample check-in and check-out (check_in_out) table:


This is the SQL query:
SELECT
 *,
 IF(`cio`.`out` > `sd`.`ends`,(DATEDIFF(`sd`.`starts`,`cio`.`in`) + DATEDIFF(`cio`.`out`,`sd`.`ends`)),DATEDIFF(`sd`.`starts`,`cio`.`in`)) AS `low`,
 
 IF(`cio`.`out` > `sd`.`ends`,(DATEDIFF(`cio`.`out`, `sd`.`starts`) - DATEDIFF(`cio`.`out`,`sd`.`ends`)), DATEDIFF(`cio`.`out`, `sd`.`starts`)) AS `high`
 
 FROM
 `check_in_out` AS `cio`,
 `season_dates` AS `sd`
WHERE
 (`cio`.`in` >= `sd`.`starts` AND `cio`.`in` <= `sd`.`ends`)
 OR 
 (`cio`.`out` >= `sd`.`starts` AND `cio`.`out` <= `sd`.`ends`)

This is the query result:


No comments:

Post a Comment