How to GROUP multiple fields in MySQL GROUP

Source: Internet
Author: User

In SQL grouping, we all use GROUP BY. For a single one, we can directly GROUP BY XXX. But how do we operate multiple fields, I will share with you some tips.

In mysql statements, group by is easy to understand as group query.
For example, select sum (score) from user group by name indicates to query the total score of each person.
However, what about select sum (score) from user group by item and sex?
The following is the test result.

The test data is as follows:

The query result is as follows:

Result Analysis
The meaning of multiple fields in group by is that they are grouped by different combinations of each field.
In this example, item = math sex = 1 and item = math sex = 2 are allocated to different groups for calculation and purpose.

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.