I have a field date_purchased and date_ordered which are datetime fields. Now I need to fetch the record if the product was purchased after order, and if the puchase was 2 days earlier or 7 days after order:
if(date_purchased>date_ordered)
{
if(date_purchased>(today-2) or date_puchased<(date_ordered+7))
}
Basically, how do I translate the above code to mysql where condition( nested and/or)?
Use
DATE_SUB()andDATE_ADD()to add days to a datetime field. The two date comparisons are enclosed in()as a single component to theAND.Note If these are
DATETIMEfields rather thanDATEfields, you may want to strip off the time portion withDATE()so that the day comparisons start at the beginning of the day, rather than some 24 hour interval from the current time. This also means usingCURDATE()instead ofNOW()to get today’s date rather than a timestamp.