SQL query to return records from the past X number of days

Posted on 2011-05-06
Last Modified: 2012-05-11
Im trying to write an SQL query to return records from the past X number of days
I saw the following  answer but when  writing this query (directly to the database in phphmyadmin)
its say DATEADD doesnt exist
select * from myTable where OrderDate >= DATEADD(d, -30, getdate())
Question by:Nura111
    LVL 9

    Expert Comment

    DATEADD in "mysqlese" is ADDDATE :) Sorry, didn't make it up.
    you can review and learn about MySQL date and time  functions here:
    LVL 9

    Expert Comment

    ... assign  points?
    LVL 21

    Accepted Solution

    In mysql we have DATE_ADD and DATE_SUB, you should use DATE_SUB.

    select * from myTable where OrderDate >= DATE_SUB(d, interval 30 days);

    Also, date_sub will return datetime; if you want to compare date you will need:

    select * from myTable where OrderDate >= DATE(DATE_SUB(d, interval 30 days));

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    I have been using r1soft Continuous Data Protection ( for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now