Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to query mysql on the current week?

Tags:

sql

mysql

This is my query for the current day:

SELECT COUNT(*) FROM blog_posts WHERE postStatus = "pending" AND DATE(date_accepted) = CURDATE()

now how about if I want to query for this week? Thanks in advance..

like image 727
user3818592 Avatar asked Sep 23 '14 11:09

user3818592


People also ask

How do I get the current week in MySQL?

MySQL WEEK() Function The WEEK() function returns the week number for a given date (a number from 0 to 53).

How do I query the current week in SQL?

datepart(dw, getdate()) will return the number of the day in the current week, from 1 to 7, starting with whatever you specified using SET DATEFIRST. dateadd(day, 1-datepart(dw, getdate()), getdate()) subtracts the necessary number of days to reach the beginning of the current week.

How can I get current week data?

It is very easy to get current week data in MySQL. Here is the SQL query to get records of current week in MySQL. In the above query, we use now() function to get present date, and week() function to get week number of date values. So we select rows whose order_date's week number is same as week number of today's day.

How do I get the current date in MySQL query?

MySQL CURDATE() Function The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.


1 Answers

Use the YEARWEEK() function

WHERE YEARWEEK(date_accepted) = YEARWEEK(NOW())

Don't just use WEEK() because that will match weeks from different years.

like image 125
Barmar Avatar answered Oct 21 '22 02:10

Barmar