SQL history query

Asked

Viewed 318 times

0

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

  • 3

    What have you achieved so far? Show us the query you tried.

  • 3

    Take advantage and account which database you are using.

1 answer

8


If you want to search from the current date, you do not need to enter any date variable. Mysql is smart :), and is able to account for when it is seven days in the past.

Do so:

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

BETWEEN returns what is 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 data works like this:

SELECT GETDATE()

Current Date less 7 days:

SELECT DATEADD(dd, -7, GETDATE())
  • 4

    As long as it’s Mysql @Brunão is using.

  • Exactly I hadn’t thought of that

  • Thank you very much Otto.... I am using SQL Server...

  • Any idea how to do this by sending the parameters via Java?

  • 1

    If you will do in the query no matter the programming language you are using Right ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.