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
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.