MySQL statistical function records

Source: Internet
Author: User

Summary by year, statistics:

Select sum (mymoney) as totalmoney, count (*) as sheets from mytable group by date_format (col, '% y ');

Monthly summary, statistics:

Select sum (mymoney) as totalmoney, count (*) as sheets from mytable group by date_format (col, '% Y-% m ');

Quarterly summary and statistics:

Select sum (mymoney) as totalmoney, count (*) as sheets from mytable group by concat (date_format (col, '% y'), FLOOR (date_format (col, '% m') + 2)/3 ));

Select sum (mymoney) as totalmoney, count (*) as sheets from mytable group by concat (date_format (col, '% y'), FLOOR (date_format (col, '% m') + 2)/3 ));

By hour:

Select sum (mymoney) as totalmoney, count (*) as sheets from mytable group by date_format (col, '% Y-% m-% d % H ');

Query data for the current year:

SELECT * FROM mytable WHERE year (FROM_UNIXTIME (my_time) = year (curdate ())

Number of accompanying quarters of query data:

SELECT id, quarter (FROM_UNIXTIME (my_time) FROM mytable;

Query data for the current quarter:

SELECT * FROM mytable WHERE quarter (FROM_UNIXTIME (my_time) = quarter (curdate ());

Statistics for this month:

Select * from mytable where month (my_time1) = month (curdate () and year (my_time2) = year (curdate ())

Weekly Statistics:

Select * from mytable where month (my_time1) = month (curdate () and week (my_time2) = week (curdate ())

Records within N days:

WHERE TO_DAYS (NOW ()-TO_DAYS (time field) <= N

  • 1
  • 2
  • 3
  • 4
  • 5
  • Next Page
[Content navigation]
Page 1: Time period Statistics Page 1: GROUP_CONCAT () function
Page 1: count by condition Page 4th: Join details
Page 1: Numeric Functions

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.