How does mysql obtain the first day of the first week of this month?

Source: Internet
Author: User
How does mysql obtain the first day of the first week of this month? I just want to talk about my thoughts: SQL statement efficiency problems, try to use mysql functions as few as possible in SQL statements, so it is better to splice SQL statements and directly query more efficiently! How do you use mysql to obtain the number of the first day of the first week of this month?
How does mysql obtain the first day of the first week of this month?

------ Solution --------------------
I just want to talk about my thoughts:
SQL statement efficiency problem, try to use mysql functions as few as possible in SQL statements, so it is better to splice SQL statements and directly query more efficiently!

You splice the first day of the first week of each month in your preferred language and then query
------ Solution --------------------
Haha, this can be used for exercises. you can familiarize yourself with the datetime series functions of mysql,
Do not rush to close the post to see who can write the simplest (the requirement is to return the datetime type value of the first Monday of the month on a certain date)

I get one of the following:
Set @ a = '2017-12-31 '-- this is just for convenience. you can also directly write the date into the SQL
;
Select date_add (last_day (date_sub (@ a, interval 1 month )),
Interval dayofmonth (date_sub (last_day (@ a), interval weekday (last_day (@ a) % 7 day)
As result
;


In addition, this is very simple in php and can be written as follows:
Echo date ('Y-m-D', strtotime ('First Monday of 2010-12-24 '));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.