How to search for saved records in the current week?

Asked

Viewed 1,527 times

9

I’m doing a database query, and I plan to list the records for the current week. It would be something based on the question about retrieve data from last 7 days from current date, but instead of being from the current date, it would be the beginning of the week.

See below the table columns tbl_vacina:

+------------+--------------+-------------------+
|     ID     |    vacina    |    data_criacao   |
+------------+--------------+-------------------+
|     1      |    Cinomose  |2017-06-10 10:11:15|
+------------+--------------+-------------------+
|     2      | Coronavirose |2017-06-09 10:11:15|
+------------+--------------+-------------------+
  • id - int
  • vacina - varchar(512)
  • data_criacao - timestamp

How would a query to return all records saved in the current week?

2 answers

13


I would suggest the use of function YEARWEEK()

SELECT *
FROM   tblVacina
WHERE  YEARWEEK(data_criacao, 1) = YEARWEEK(CURDATE(), 1)

(I’m trying to create Sqlfiddle but I can’t execute)

  • Confirmed, it works. kkk I should have insisted more on this solution, it’s even more elegant. I had already voted +1 on it.

  • Thank you, there are two versions (+1):)

  • I find this the most suitable to be marked as accepted

  • 1

    It worked beauty! Thank you!

9

If you were to compare whether two arbitrary dates are in the same week:

SELECT
   campos
FROM
   tabela
WHERE
   DATE_SUB(data1, INTERVAL DAYOFWEEK(data1)-1 DAY)
   =
   DATE_SUB(data2, INTERVAL DAYOFWEEK(data2)-1 DAY);

Applying to your case:

WHERE
   DATE_SUB(data_criacao, INTERVAL DAYOFWEEK(data_criacao)-1 DAY)
   =
   DATE_SUB(CURRENT_DATE, INTERVAL DAYOFWEEK(CURRENT_DATE)-1 DAY);

Basically, if we take a date and extract the day of the current week, we have the Sunday immediately before. If it’s the same in both cases, it’s the same week.


If you don’t have future data in DB, you can simplify it like this:

SELECT
   campos
FROM
   tabela
WHERE
   data_criacao >= DATE_SUB(CURRENT_DATE, INTERVAL DAYOFWEEK(CURRENT_DATE)-1 DAY)

As we are using today’s date as a reference, it is sufficient that the data_criacao is the same or younger than last Sunday.

  • Bacco, "if we take a date and extract the current day of the week, we have the Sunday immediately before" I did not understand well, can explain to me sff

  • @Miguel if today is Tuesday, and I take 3 of today’s date, falls on the same day as take the Thursday date and take 5. DAYOFWEEK numerically returns the day of the week.

  • But you’re always subtracting one, which means if today is Tuesday it’s Monday, right?

  • is subtracting DAYOFWEEK(CURRENT_DATE)-1 - This DAYOFWEEK returns the day of the week from 1 to 7; the -1 is just to transform from 0 to 6.

  • Ha ok got it, obagdo Bacco

  • Bacco thank you! It worked beauty but really the answer of Bruno works too and is simpler.

Show 1 more comment

Browser other questions tagged

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