[英]How to get previous year from day of week and week
I have function get day of week and week from current day in mysql It look like.我有函数在 mysql 中从当前日期获取星期几和星期看起来像。
select
DATE_FORMAT(now(), '%U') as w,
DATE_FORMAT(now(), '%w') as day_of_w;
It return w is 37 and day_of_week is 1. How to get correct value w is 37 and day_of_week is 1 of previous year.它返回 w 是 37 并且 day_of_week 是 1。如何获得正确的值 w 是 37 并且 day_of_week 是前一年的 1。
I using我用
select
DATE_FORMAT(now() interval 1 year, '%U') as w,
DATE_FORMAT(now() interval 1 year, '%w') as day_of_w;
But SQL cannot execute.但是SQL不能执行。
The function to turn a string into a date is STR_TO_DATE
.将字符串转换为日期的函数是
STR_TO_DATE
。 If you want to get the date for week 37, day 1 in 2019, you could use如果要获取 2019 年第 37 周第 1 天的日期,可以使用
select str_to_date('37 1 2019', '%U %w %Y')
If you want the date for today's week and day number in last year:如果您想要今天的星期几和去年的天数的日期:
select str_to_date(concat_ws(' ', date_format(current_date, '%U'),
date_format(current_date, '%w'),
year(current_date) - 1),
'%U %w %Y')
Be aware though, that some years have a week 53 while others don't.但请注意,有些年份有 53 周,而有些年份则没有。 If you run this query in a year's 53rd week, you don't get a valid result.
如果您在一年的第 53 周运行此查询,则不会得到有效结果。
select DAYOFWEEK(now()) day_of_week, week(now()) week from dual
your query has syntactical problem.您的查询存在语法问题。 you forgot +/- symbol
你忘记了 +/- 符号
select DATE_FORMAT(now() + interval 1 year, '%U') as w
,DATE_FORMAT(now() + interval 1 year, '%w') as day_of_w;
You just need to use DATE_ADD
function in MYSQL
to change the date to last year.您只需要在
MYSQL
使用DATE_ADD
函数将日期更改为去年。
select DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%U') as w
,DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%w') as day_of_w;
Result:结果:
36 6
Current and last year date:当前和去年日期:
select now() as curr_yr_date, DATE_ADD(now(), INTERVAL -1 YEAR) as last_yr_date;
Result:结果:
2020-09-14 07:13:24 2019-09-14 07:13:24
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.