Get current week’s records

Asked

Viewed 34 times

0

I have a system where the user registers the information in a Mysql database. One of the fields is Datacadastro of the kind Date() and that is registered only from Monday to Friday, that is, in the days that this user’s company works. I need to get just the current week’s records. I understand that to get the next 7 days, I have to do this:

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

But how would I get only the dates of the current week?

1 answer

2


Solution 1:

Since it is Mysql, you can use the function YEARWEEK(), just put the date inside the function YEARWEEK('2018-07-24')

SELECT * FROM your_table WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)

Solution 2:

SELECT * FROM items WHERE created_at > DATE_SUB(NOW(), INTERVAL 1 WEEK);
  • Perfect fabionvs. The first solution fell better in my application. Thank you very much!

Browser other questions tagged

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