Avatar of grantunwin
grantunwin
 asked on

MySQL Problem

In a solution provided in a previous question I was given this:

$rd = $_REQUEST['start_date'];
$sql = "SELECT * FROM `listings` WHERE `date` >= date_add(str_to_date('$rd', '%d/%M/%Y'), interval -3 day) and `date` < date_add(str_to_date('$rd', '%d/%M/%Y'), interval 4 day) ";

I need to know if this would work:


$rd = $_REQUEST['start_date'];
$sql = "SELECT * FROM `listings` WHERE str_to_date(`date`, '%d/%M/%Y') >= date_add(str_to_date('$rd', '%d/%M/%Y'), interval -3 day) and str_to_date(`date`, '%d/%M/%Y') < date_add(str_to_date('$rd', '%d/%M/%Y'), interval 4 day) ";

MySQL ServerPHPWeb Development

Avatar of undefined
Last Comment
cyberkiwi

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
cyberkiwi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
cyberkiwi

Using any function on the `date` column also causes an index on that column to be unusable.  Consider making a date range that will cover the date+time involved.
If `date` contains time data and you want to include the 7th day, then try this

$rd = $_REQUEST['start_date'];
$sql = "SELECT * FROM `listings` WHERE `date` >= date_add(str_to_date('$rd', '%d/%M/%Y'), interval -3 day) and `date` < date_add(str_to_date('$rd', '%d/%M/%Y'), interval 5 day) ";

< ".. interval 5 days "

will include any hour on the day "$rd +4 days"
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23