SQL History Query

0

I'm in need of a help with SQL. I need to do a query that returns me a history of the current date up to 7 days back. Could someone please help me?

    
asked by anonymous 18.09.2014 / 14:26

1 answer

8

If you want to fetch from the current date, you do not need to enter any date variables. MySQL is smart :), and is able to make the count of when it's seven days in the past.

Do this:

SELECT * 
FROM tabela 
WHERE 
  data BETWEEN CURRENT_DATE()-7 AND CURRENT_DATE()  

BETWEEN returns what's between these dates.

CURRENT_DATE() returns the current date.

CURRENT_DATE()-7 returns the current date minus seven days.

References:

In SQL Server the function for date works like this:

SELECT GETDATE()

Current date at 7 days:

SELECT DATEADD(dd, -7, GETDATE())
    
18.09.2014 / 14:30