MySQL time and date calculation function application

Source: Internet
Author: User
Tags datetime time and date

Calculate the number of years
If you want to calculate the age of the person by birth, you can use the following statement:

SELECT Date_format (From_days to_days (now ())-To_days (@dateofbirth)), '%Y ') + 0;

2. Two difference in time
Gets the difference of two datetime values. Assuming that DT1 and DT2 are datetime types, and their format is ' Yyyy-mm-dd hh:mm:ss ', then the difference in seconds between them is: Unix_timestamp (DT2)-Unix_timestamp (DT1) divided by 60 is the bad The number of minutes divided by 3600 is the difference between the number of hours, divided by 24 is the difference in the number of days.
3. Show the value of n times a column appears
The code is as follows:

SELECT ID
From TBL
GROUP by ID
Having COUNT (*) = N;

4. Calculation of working days between two days
The so-called working day is the addition of Saturday Sundays and holidays.
The code is as follows:

SELECT COUNT (*)
From calendar
WHERE d BETWEEN Start and Stop
and DayOfWeek (d) not in (1,7)
and holiday=0;
Related Article

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.