Detailed explanation of MySQL data grouping

Detailed explanation of MySQL data grouping

Create Group

Grouping is established in the GROUP BY clause of the SELECT statement.

example:

SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id;

GROUP BY

  • The GROUP BY clause can contain any number of columns, which allows nested groupings and provides more detailed control over data grouping.
  • If you nest groups in the GROUP BY clause, the data will be aggregated at the last specified grouping. In other words, when creating a grouping, all specified columns are calculated together. (So ​​data cannot be retrieved from individual columns).
  • Each column listed in the GROUP BY clause must be a search column or a valid expression (but not an aggregate function). If you use an expression in the SELECT clause, you must specify the same expression in the GROUP BY clause. You cannot use an alias.
  • Except for aggregate calculation statements, every column in the SELECT statement must be given in the GROUP BY clause.
  • If there is a NULL value in the grouping column, NULL will be returned as a group. If there are multiple rows with NULL values ​​in the column, they will be grouped together.
  • The GROUP BY clause must appear after the WHERE clause and before the ORDER BY clause.

Filter Groups

If one wants to list all orthopedics departments that have at least two orders, the data must be filtered based on the complete grouping rather than individual rows.

You can use HAVING

SELECT cust_id, COUNT(*) AS orders FROM orders GROUP BY cust_id HAVING COUNT(*) >= 2;

The difference between HAVING and WHERE is that WHERE filters before data grouping, while HAVING filters after grouping. Of course, both can appear in the same statement.

List suppliers with 2 or more products with a price of 10 or more

SELECT vend_id, COUNT(*) AS num_prods FROM products WHERE prod_price >= 10 GROUP BY vend_id HAVING COUNT(*) >= 2;

List suppliers with two top products

SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id HAVING COUNT(*) >= 2;

Grouping and Sorting

ORDER BY GROUP BY
Sorting the output Group rows. But the output may not be in the order of the groups
Any column can be used (even non-selected columns) Only select columns or expression columns may be used, and each select column expression must be used.
Not necessarily required If you use a column (expression) with an aggregate function, you must use

Retrieve the order number and total order price of orders with a total order price greater than or equal to 50

SELECT order_num, SUM(quantity*item) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price) >= 50;

If you sort the output by total order price

SELECT order_num, SUM(quantity * item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity * item_price) >= 50 ORDER BY ordertotal;

Order of SELECT clauses

Clauses illustrate Is it necessary to use
SELECT The column or expression to return yes
FROM The table from which to retrieve data Only used when selecting data from a table
GROUP BY Group Description Only used when computing aggregates by group
WHERE Row-level filtering no
HAVING Group level filtering no
ORDER BY Output sort order no
LIMIT The number of rows to retrieve no

The above is a detailed explanation of MySQL data grouping. For more information about MySQL data grouping, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySql grouping and randomly getting one piece of data from each group
  • Implement group by based on MySQL to get the latest data of each group
  • MySQL subqueries and grouped queries
  • MySQL grouping queries and aggregate functions
  • MySql Group By implements grouping of multiple fields
  • Detailed explanation of MySQL group sorting to find the top N
  • Detailed example of getting the maximum value of each group after grouping in MySQL
  • Mysql uses group by group sorting

<<:  Analysis of the usage of Xmeter API interface testing tool

>>:  Markup Language - Image Replacement

Recommend

HTML checkbox Click the description text to select/uncheck the state

In web development, since the checkbox is small an...

Common symbols in Unicode

Unicode is a character encoding scheme developed ...

Linux installation MySQL5.6.24 usage instructions

Linux installation MySQL notes 1. Before installi...

Detailed examples of variable and function promotion in JavaScript

js execution Lexical analysis phase: includes thr...

3 ways to create JavaScript objects

Table of contents 1. Object literals 2. The new k...

Recommended plugins and usage examples for vue unit testing

Table of contents frame First-class error reporti...

In-depth understanding of MySQL slow query log

Table of contents What is the slow query log? How...

jQuery achieves large-screen scrolling playback effect

This article shares the specific code of jQuery t...

Detailed steps to install RabbitMQ in docker

Table of contents 1. Find the mirror 2. Download ...

Use of hasOwnProperty method of js attribute object

Object's hasOwnProperty() method returns a Bo...

HTML+CSS to achieve text folding special effects example

This article mainly introduces the example of rea...

CSS to achieve glowing text and a little bit of JS special effects

Implementation ideas: Use text-shadow in CSS to a...

MySQL 8.0.12 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Example of how to configure the MySQL database timeout setting

Table of contents Preface 1. JDBC timeout setting...