MySQL Tutorial: GroupBy usage _ MySQL

Source: Internet
Author: User
Tags mysql tutorial
MySQL Tutorial: the usage of GroupBy is now back to the function. Remember to use the SUM command to calculate all Sales (turnover! What should we do if we want to calculate the turnover (sales) of each store (store_name? In this case, we need to do two things: first, we need to select both store_name and Sales columns. Second, we need to confirm that all sales are calculated separately based on each store_name. The syntax is:

SELECT "column 1", SUM ("column 2") FROM "table name" group by "column 1"

SELECT store_name, SUM (Sales) FROM Store_Information group by store_name

In our demonstration,

Store_Information table

Store_name Sales Date

Los Angeles $1500 Jan-05-1999

San Diego $250 Jan-07-1999

Los Angeles $300 Jan-08-1999

Boston $700 Jan-08-1999

Let's get
SELECT store_name, SUM (Sales) FROM Store_Information group by store_name

Result:

Store_name SUM (Sales)

Los Angeles $1800

San Diego $250

Boston $700

When we select more than one column and at least one column contains the function, we need to use the group by command. In this case, we need to determine that we have all other columns of group. In other words, we need to put all the columns including functions in the group by clause.

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.