Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:20:53+00:00 2026-06-03T09:20:53+00:00

I have a field date_purchased and date_ordered which are datetime fields. Now I need

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:20:54+00:00Added an answer on June 3, 2026 at 9:20 am

    Use DATE_SUB() and DATE_ADD() to add days to a datetime field. The two date comparisons are enclosed in () as a single component to the AND.

    WHERE
      date_purchased > date_ordered
      AND (
        date_purchased > DATE_SUB(NOW(), INTERVAL 2 DAY) 
        OR date_purchased  < DATE_ADD(date_ordered, INTERVAL 7 DAY)
      )
    

    Note If these are DATETIME fields rather than DATE fields, you may want to strip off the time portion with DATE() 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 using CURDATE() instead of NOW() to get today’s date rather than a timestamp.

    WHERE
      date_purchased > date_ordered
      AND (
        DATE(date_purchased) > DATE_SUB(CURDATE(), INTERVAL 2 DAY) 
        OR DATE(date_purchased)  < DATE_ADD(DATE(date_ordered), INTERVAL 7 DAY)
      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datetime field like that. <field name=rel_date type=date indexed=true stored=true default=NOW multiValued=false/>
I have a huge database (800MB) which consists of a field called 'Date Last
I have a products table, with the fields product, category and cost, of type
I have a query which returns 3 fields, one of which is the month
I'm selecting data from two different tables which have a common field date and
I have a date field in php which is using this code: $date =
We have a date field in one of our tables and i need to
I have a field called submit_date which stores the value of a timestamp. The
I have date field in my MySQL DB and three fields (yyyy-mm-dd) in my
I have an input field with onChange event which triggers an AJAX call which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.