A tutorial on using the SUM function in MySQL

Source: Internet
Author: User
Tags mysql tutorial

This article mainly introduces the use of the SUM function in MySQL tutorial, is the basic knowledge of MySQL introductory learning, the need for friends can refer to the

The sum function of MySQL is used to find the sum of the various fields in the record.

To understand the SUM function consider the EMPLOYEE_TBL table has the following records:

?

1 2 3 4 5 6 7 8 9 10 11 12-13 Mysql> SELECT * from EMPLOYEE_TBL; +------+------+------------+--------------------+ | ID | name | Work_date | Daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in Set (0.00 sec)

Now, assuming that you want to calculate the total number of all dialy_typing_pages based on the table above, you can use the following command:

?

1 2 3 4 5 6 7 8 Mysql> SELECT SUM (daily_typing_pages)-> from Employee_tbl; +-------------------------+ | SUM (daily_typing_pages) | +-------------------------+ | 1610 | +-------------------------+ 1 row in Set (0.00 sec)

The sum of the various records that can be set by using the GROUP BY clause. The following example will summarize all the relevant records of a person, the total number of paper printed on each person.

?

1 2 3 4 5 6 7 8 9 10 11-12 mysql> SELECT name, SUM (daily_typing_pages)-> from Employee_tbl GROUP by name; +------+-------------------------+ | name | SUM (daily_typing_pages) | +------+-------------------------+ | Jack | 270 | | Jill | 220 | | John | 250 | | Ram | 220 | | Zara | 650 | +------+-------------------------+ 5 rows in Set (0.17 sec)
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.