mysql - How to get last Sunday and Sunday before last using MySQL?
问题描述
I am trying to get the date of last Sunday and the date of the Sunday before that.
For example:
Today (Tuesday) is '20190604'
@Last_Sunday = '20190602'
@Sunday_B4_Last = '20190526'
I know how to transfer the date to ISO format but i do not know how to get it to show Sundays.
I found answers to the same question but in other languages like PostgreSQL and JS but nothing for MySQL.
I found some code online that does something very similar but it shows Friday and I was not able to make it show the Friday before that.
Here is the code:
select now() - interval (weekday( now())+ 2)% 7+ 1 day 'Previous Friday';
I would appreciate any answers but especially ones that explain HOW to manipulate the formula above or any other formula that gives the desired result.
解决方案
An option:
SELECT
`dates`.`date` - INTERVAL WEEKDAY(`dates`.`date`) % 7 + 1 DAY `Last`,
`dates`.`date` - INTERVAL WEEKDAY(`dates`.`date`) % 7 + 8 DAY `Previous`
FROM (
SELECT '2019-06-04' `date`
) `dates`;
See dbfiddle.
推荐阅读
- android - Kotlin 谷歌地图无法启动
- javascript - VSCODE Javascript模板字符串以不确定的方式变为白色并丢失突出显示
- c# - C# 反序列化 - 获取属性返回 null
- wpf - 如何在 wix 工具集引导程序项目中获取当前的 wpf 项目程序集版本?
- c# - 将数据输入文本框后如何自动显示数据?C#
- c# - 使用 Linq 将字符串解析为 int[]
- php - DOMDocument - 在不添加命名空间的情况下获取节点 XML
- javascript - 在 javascript 文件中使用 maven 原型模板变量
- python - 在 Python 中使用函数的新手
- python-3.x - 玩家和电脑尝试删除1-3,直到数字达到0。如何确保电脑每次都获胜?