MySQL Select week number within current month - mysql

I have a table with events, some of which are recurring at spesific weeks within a month (Like, the 1st wednesday every month).
How would one select the below?
table
event day week
quiz wednesday 1
pseudocode would be:
SELECT * FROM table WHERE week = weeknumberWithinCurrentMonth()
Since we are now in the third week of february, 0 rows would be fetched from the above pseudoquery. If we were in week 1 of february, it would have selected the example row.

SELECT * FROM table WHERE week = FLOOR((DayOfMonth(NOW())-1)/7)+1
As close as you can get.

This may be what you are looking for:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
SELECT WEEK(CURDATE()) - WEEK(DATE_FORMAT(NOW() , '%Y-%m-01')) + 1

Related

MySQL-Get latest data of 1st 4 days of current month

I have a table in my database which stores the meters energy value of 1st of every month. In case meter is offline it will store the value of the next day.
Below is my case
I have a record of a meter of past 2 months February and March. The February data is of 2019-02-01 00:00:00 but there are 4 rows for the month March. See the below image
In the above image the 1st,2nd and 3rd of March have a null value of FA but the 4th March contains some value.
What I have done?
I am able to select the rows having values of FA.
What I want to do?
I want to get only the current month data i.e. Current month is March so it should get only march record and then next month it should get only April record and so on.
The query should not exceed the days limit more than 4 i.e. It should only check record for 1st four days of every month.
Here is my DB-Fiddle
Any help would be highly appreciated.
one way to solve this is
FOR
I want to get only the current month data i.e. Current month is March so it should get only march record and then next month it should get only April record and so on.
means month(TV)= month(now())
and
The query should not exceed the days limit more than 4 i.e. It should only check record for 1st four days of every month. means day(TV)<= 4
and finally your query
select * from `biz_pub_data_f_energy_m` a
where a.`DATA_ID` = '1b9716122dd5408691a063227316ac0a'
and a.`FA` is NOT NULL and month(TV)= month(now())
and day(TV)<= 4
You can try below -
DEMO
select * from `biz_pub_data_f_energy_m` a
where a.`DATA_ID` = '1b9716122dd5408691a063227316ac0a'
and a.`FA` is NOT NULL and tv>=date(DATE_SUB(now(),INTERVAL DAYOFMONTH(now())-1 DAY))
and tv<=DATE_ADD(date(DATE_SUB(now(),INTERVAL DAYOFMONTH(now())-1 DAY)), INTERVAL 4 DAY)

Getting only the data from the first day of the week to the current day of the week

So I want to select all my row that is recorded by this week.
For example. today is January 30 2019, which is Wednesday. I would like to select all my row from Monday(January 28,2019) to Wednesday only(current day). And when tomorrow comes, the rows added on Thursday will also be selected.
This will continue til Sunday. And when the Next week Monday comes, it will only select the rows recorded on that Monday. (February 4, 2019) and so on.
I just need to get the (THIS WEEK) data, not the past 7 days.
I have a dateadded column
-----------------------------
Firstname|Lastname|dateadded
Michael |Jordan |2019-02-03 <-(Feb 3, 2019)
Mark |Perez |2019-01-30
Paul |George |2019-01-28
John |Wayne |2019-01-25
-----------------------------
A query that will only select this weeks added data.
Mark Perez and Paul George which is in the same week of the current day.
select * from names WHERE YEARWEEK(`dateadded`, 1) = YEARWEEK(CURDATE(), 1)
will only select the last 7 days of the curdate.
Well you can use WEEKDAY() to get the day number of the current day within the week, and susbstract if from the current date :
SELECT SUBDATE(CURDATE(), WEEKDAY(CURDATE()))
Yields :
2019-01-28
Demo on DB Fiddle
As commented by Nick, here is an example of a query that uses the above approach to filter a table :
SELECT t.*
FROM test t
WHERE t.idate >= SUBDATE(CURDATE(), WEEKDAY(CURDATE()))
ORDER BY t.idate
Demo on DB Fiddle
You have example of how your table looks?
You could insert records with date and week numbers.
If you record days by week numbers it's easy like :
SELECT * FROM weektable WHERE weekno='5';
.. There are 52 weeks in a year ..
This will select everything from week number 5 and will be adding new rows from same week number automatically.

Selecting mysql records for next week

I'm looking for the MySQL query to select date records that fall within the next week using Sunday - Saturday as the format for the week.
So in other words, I'm not looking to get the dates a week from today, I'm looking to get the dates that fall within Sunday - Saturday of the next week.
I found this: MySQL Query to select data from last week? and it works for the previous week from Sunday - Saturday but I'm not sure how to tweak this to get the dates for the next week.
Any ideas?
SELECT * FROM table
WHERE
date > date_add(curdate(),INTERVAL(7-dayofweek(curdate()))DAY)
AND date <= date_add(curdate(),INTERVAL(14- dayofweek(curdate()))DAY)
You can use:
SELECT *
FROM YourTable
WHERE WEEK(YourDateField, 6) = WEEK(CURRENT_DATE + INTERVAL 7 DAY, 6)
for selecting recods of next week (starting with sunday)
Weeks count is 1 to 53 with first week of the year having at least 4 days in the year

week number of a date between 2 dates

I need to get the week number between 2 dates, considering the week ends on Sunday ( may not be 7 days )
for clarification assume the dates are (01-Apr-2014, 31-May-2014)
so the date ( 03-Apr-2014) should be in the first week
and the date ( 06-Apr-2014) will be the first day of the second week and so on.
thanks on advance
SELECT YEARWEEK(#SoughtDate) - YEARWEEK('2014-04-01') + 1

Select dates within the last week, but not only last 7 days

I want to select all dates within the last week.
But not by simply counting the current date - 7, as all posts Ive come across suggest.
This is how I actually have it for the SUM now....
SELECT SUM(total) FROM payforms WHERE user_id = 1 GROUP BY WEEK(date)
This gives me a nice total...
But I want to retrieve all individual records within the last week.
So I can use a BETWEEN query....but how do I get it to look in the current week.
Example...
Tuesday I want it to only find values from Sun, Mon, Tue.
On wednesday, I want it to find Sun, Mon, Tue, Wed.
On Saturday it finds the whole previous week. etc
So to be more clear....
I dont want it to find last Monday, on a monday.
On Mondays it should only display Mondays, if you get what I mean.
Can I do this??
Thanks
select * from payforms where yearweek(date) = yearweek(now());
Although I think MySQL weeks start on Sunday
You can make use of the WEEKDAY date function, which returns the day index of the week.
If I understand your question correctly, you could use this query:
SELECT date, SUM(total)
FROM payforms
WHERE date >= CURDATE() - INTERVAL DAYOFWEEK(CURDATE())-1 DAY
GROUP BY date
Please see fiddle here.
This will SUM all totals for every day of the current week, starting on last Sunday. If you want it to start on last Monday you can use this:
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY