Jump to content

[SOLVED] SQL BETWEEN Problem


gerkintrigg

Recommended Posts

I'm trying to check whether a property is available during a specified range of dates using this code:

<?php 
$avail_query="SELECT COUNT(*) as records FROM booking, cottage WHERE 
	((
	(Date_ADD( start_date, INTERVAL 1
DAY ) ) BETWEEN '$week_start2' && '$week_end2') 
	|| ((Date_SUB( end_date, INTERVAL 1
DAY ) ) BETWEEN '$week_start2' && '$week_end2') 
	|| ('$week_start2' BETWEEN (Date_ADD( start_date, INTERVAL 1
DAY ) ) && (Date_SUB( end_date, INTERVAL 1
DAY ) )) 
	|| ('$week_end2' BETWEEN (Date_ADD( start_date, INTERVAL 1
DAY ) ) && (Date_SUB( end_date, INTERVAL 1
DAY ) )))
	&& cottage.name='$cot'
	&& cottage.cottage_id=booking.cottage_id";
?>

 

It's not finding some over-lapping date ranges that should have been returned.

Can anyone help please?

Link to comment
https://forums.phpfreaks.com/topic/57243-solved-sql-between-problem/
Share on other sites

I tried it on my MySQL and it does produce an error.  That's why I brought it up, and that's why you come across someone asking about that.  But if you're getting results, clearly we have different SQL servers/versions.

mysql> SELECT 2 BETWEEN 1 && 4;                                                                        
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near '&& 4' at line 1
mysql> SELECT 2 BETWEEN 1 AND 4;                                                                      
+-------------------+
| 2 BETWEEN 1 AND 4 |
+-------------------+
|                 1 | 
+-------------------+
1 row in set (0.00 sec)

 

So you're trying to see if either date is within the range of other dates, non-inclusive?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.