select current week dates starting from saturday - mysql

I want id whose date join is saturday to friday night.
SELECT id
FROM signup
WHERE yearweek(DATE(datejoin), 6) = yearweek(curdate(), 6)

You can use WeekDay function, this function returns an Integer value for every days of week from 0 for Monday to 6 for Sunday, if you subtract WeekDay from current date you will get the date of last Monday, if you want the date of the next Friday you can get it with a query like this :
CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 4 DAY
In you query you want to start your week from Saturday to Friday so result of your query depends on the current day of week, if the current day of week is before Friday(from 0 to 4), you should get the date of next Friday but if current day of the week is after Friday(5 or 6) you should get the date of two Friday next !, the end of your week (Saturday) has this situation too
Look at this query, I hope it's clear enough to you :
SELECT id FROM signup WHERE
datejoin BETWEEN
(CASE
WHEN WEEKDAY(CURDATE()) > 4 THEN
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 5 DAY)
ELSE
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) + 2 DAY)
END)
AND
(CASE
WHEN WEEKDAY(CURDATE()) > 4 THEN
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 11 DAY)
ELSE
(CURDATE() - INTERVAL WEEKDAY(CURDATE()) - 4 DAY)
END)

Answer:
SELECT id from signup where datejoin between (CURRENT_DATE - INTERVAL 7 DAY) AND CURRENT_DATE

Related

Get the data of the last week from (Monday to Sunday)

I would like to get the data from the last week. A week is Monday to Sunday.
I made this query :
sql = 'SELECT COUNT(*) ' \
'FROM panelname ' \
'WHERE year(date) = year(now()) ' \
'AND date BETWEEN date_sub(now(),INTERVAL 1 WEEK) AND now() '
cursor.execute(sql)
test = cursor.fetchall()
print(test)
The problem with AND date BETWEEN date_sub(now(),INTERVAL 1 WEEK) AND now() is that it takes the data from the past 7 days. If I am Tuesday of the current week and I run it, I will get the data from last week's Tuesday up until this week's Tuesday, which is not what I want. It should give the data from last Monday down to last Sunday.
I also precise that it should not use the DATEADD function, because I don't have the admin rights to use it.
For example : I run the query today, it doesn't matter which day we are, and I get the last week of data (Monday to Sunday).
Thank you.
You must first get the most recent Monday and use it as a reference point to calculate the 5 previous weeks:
SELECT COUNT(*)
FROM panelname
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) day - INTERVAL 5 week
AND date < CURDATE() - INTERVAL WEEKDAY(CURDATE()) day
UPDATE
To get the aggregated count per each week you need to group by week:
SELECT WEEKOFYEAR(date),COUNT(*)
FROM panelname
WHERE date >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) day - INTERVAL 5 week
AND date < CURDATE() - INTERVAL WEEKDAY(CURDATE()) day
GROUP BY WEEKOFYEAR(date)

MySQL select complete last month

How to select all data from last month (or 30 days)?
I already found some answers, and mostly gives this solution
SELECT *
FROM gigs
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
ORDER BY date DESC
But this gives me also the dates from the future
I am only interested in the days from last month or 30 days (not next month and beyond)
Is this what you want?
WHERE date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND date <= CURRENT_DATE
I added a condition so the query filters on date not greater than today. I also modified your code so the date range starts one month ago (you had 3 months).
try this code
SELECT * FROM gigs
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
ORDER BY date DESC
You are asking for two separate things.
The last 30 days is easy.
date between now() - interval 30 day and now()
Data this month is like this:
date between (last_day(Now() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) and last_day(Now())
Data a few months ago is like this:
date between (last_day(Now() - INTERVAL 4 MONTH) + INTERVAL 1 DAY)
and
(last_day(Now() - INTERVAL 3 MONTH) + INTERVAL 1 DAY)

Get records from last one year upto last date of previous month in mysql

I am trying to get records from last one year and upto last date of provious month i.e. not including the current month. Here's my query:
SELECT `customer_id`, `customer_name`, `customer_date`
FROM `customers`
WHERE DATE(`customer_date`) <= (CURDATE() - INTERVAL 1 MONTH)
AND DATE(customer_date) >= (CURDATE() - INTERVAL 12 MONTH)
This query fetches records from 1 May, 2018 to 8 April 2019.
INTERVAL 1 MONTH fetches records 30 days ago. What I need to do something here?
I want to exclude current month records, so query should return records upto 30 April 2019. How do we do that?
You must correctly calculate the first and last days of range with help LAST_DAY() function. For example:
Calculate the first day of the range
SELECT LAST_DAY(CURDATE() - INTERVAL 13 MONTH) + INTERVAL 1 DAY
Output:
2018-05-01
Calculate last day of the range
SELECT LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
Output:
2019-04-30
The full query might look like:
SELECT `customer_id`, `customer_name`, `customer_date`
FROM `customers`
WHERE `customer_date` >= LAST_DAY(CURDATE() - INTERVAL 13 MONTH) + INTERVAL 1 DAY
AND `customer_date` <= SELECT LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
To get data up to the previous month:
where customer_date < curdate() + interval (1 - day(curdate()) day
Why? First note that there is no function call on the customer_date. So, this expression is index-compatible and can use an index.
Second, this structure works both for dates and date/times. That is very handy, because it may not always be obvious if a column has a time component (people are not very good about naming columns to capture this information).
You claim that the "12 months" ago portion works. That doesn't look correct to me. For the complete logic:
where customer_date < curdate() + interval (1 - day(curdate()) day and
customer_date >= (curdate() + interval (1 - day(curdate()) day) - interval 1 year)
SELECT `customer_id`, `customer_name`, `customer_date` FROM `customers` WHERE
MONTH(`customer_date`) <= (CURDATE() - INTERVAL 1 MONTH) AND
MONTH(customer_date) >= (CURDATE() - INTERVAL 12 MONTH)
hope this help

MySQL: select date of current week's monday

I'm building a weekly report using MySQL queries. First I get week number by
SELECT WEEK(CURDATE());
Then I need to display the starting date of the week (Monday for my region). How to do it with MySQL only?
Thanks!
If you need date of monday in current week
try this:
SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
It return you monday date of current week.
In fact, DATE_ADD is not necessary and complicates the logic. Here is the simplest version for "Date of the first day of the current week":
select date(curdate() - interval weekday(curdate()) day)
which would translate to:
get the date() part of the (current date - an interval of N days)
where N = today's number in the week, i.e. Thu is number 3 (for a week starting on Monday)
Then, getting the Monday of previous week, is:
select date(curdate() - interval weekday(curdate()) day - interval 1 week)
which is more readable IMHO.
For thhose who need the Monday date of the current week.
If you count Monday as the first day of the week:
SELECT STR_TO_DATE(CONCAT(YEARWEEK(NOW(), 1),'Monday'), '%x%v %W');
If you count Sunday as the first day of the week:
SELECT STR_TO_DATE(CONCAT(YEARWEEK(NOW()),'Monday'), '%X%V %W');
SELECT DATE_ADD((SELECT curdate() - INTERVAL (WEEKDAY(curdate())+1)DAY),INTERVAL 1 DAY) as current_monday
-- current week monday
(SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY))
-- current week friday
SELECT DATE_ADD((SELECT DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)),INTERVAL 4 DAY);
-- next week monday
SELECT date(curdate() - interval weekday(curdate()) day + interval 1 week);
-- next week friday
SELECT DATE_ADD((SELECT date(curdate() - interval weekday(curdate()) day + interval 1 week)),INTERVAL 4 DAY);
In fact, we substruct days, not add them. Just more simple expression:
DATE_SUB( CURDATE(), INTERVAL WEEKDAY( CURDATE() )
You can use this:
<?php
$week = date('W');
$year = date('Y');
echo $date1 = date(
'Y-m-d',
strtotime($year . 'W' . str_pad($week, 2, '0', STR_PAD_LEFT))
);
?>

How do I SELECT the first week of a previous month

How do I SELECT the first week of a previous month I've tried
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>=(CURDATE()- 1 WEEK - INTERVAL 2 week)";
This was used to try and select the third week but this didn't work
Does this work for you:
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>= curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week
AND startDate < curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week _+ interval 1 week";
?
The idea here is that we first go back a month, then find the start of the week (assuming Monday, for Sunday we will need some extra tweaking), then figure out how many whole weeks it has been from the start of the month, and subtract that many weeks from the date so far. This takes us back to the start of the first week of the month. For the end of the range we just add one week to the start.
Ah, your question became more detailed.
Not really familiar with sql, there might be better but something like:
SELECT repairId , startDate,catId, statusId FROM repair
WHERE EXTRACT(YEAR_MONTH FROM start_date) = EXTRACT(YEAR_MONTH FROM NOW()) - 1 AND CAST(EXTRACT(DAY FROM start_date) / 7 + 1 as INT) = ?;
Basically, extract the year month components to compare year and month and then extract the day of month use the flooring caused by integer truncation to get the week to compare with whatever week you are looking for
mysql return rows matching year month