如何在MySQL中找到每月的第一个星期日? [英] How to find the first Sunday of the month in MySQL?

查看:72
本文介绍了如何在MySQL中找到每月的第一个星期日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到MySQL的任何示例,所有示例都非常复杂。

I did not find any of examples for MySQL - all of them were quite complicated.

如何 SELECT 一个月的第一个星期日?

How can I SELECT the first Sunday of the month?

推荐答案

所以选择每月的第一天: 2012 -01-01 (或您想要的月份和年份)。

So choose the first day of the month: 2012-01-01 (or whatever month and year you want).

获取日期的工作日索引。此处的索引从0到6。

Get the weekday index of the date. Indexes here are from 0 to 6.

从6减去该索引,您将得到需要添加的天数,直到日期为星期日。

Subtract that index from 6 and you will get how many days you need to add until the date is Sunday.

将该天数添加到所选日期。

Add that amount of days to the chosen day.

SELECT DATE_ADD("2012-01-01 10:00:00", INTERVAL (6 - WEEKDAY("2012-01-01 10:00:00")) DAY);

或:

SELECT DATE_ADD("2012-01-01", INTERVAL (6 - WEEKDAY("2012-01-01")) DAY);

这篇关于如何在MySQL中找到每月的第一个星期日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆