Home > Daily Programming > Mysql Knowledge > What is the statement used to group in mysql

What is the statement used to group in mysql

下次还敢
Release: 2024-04-27 06:18:14
Original
348 people have browsed it

The GROUP BY statement in MySQL is used to group data by specified columns and calculate aggregate values ​​for each group, such as totals, sums, and averages.

What is the statement used to group in mysql

The statement used for grouping in MySQL: GROUP BY

The GROUP BY statement in MySQL is used to group data Group by specified columns and calculate aggregate values ​​for each group such as SUM, COUNT, AVG, etc.

Syntax:

<code class="sql">SELECT 列名1, 列名2, ...
FROM 表名
GROUP BY 分组列
HAVING 条件(可选)</code>
Copy after login

Parameters:

  • ##Column name 1, column name 2, . ..: The column to be selected.
  • Table name: The table to be grouped.
  • Grouping column: Column used for grouping.
  • HAVING conditions (optional): Conditions used to filter group results.

How it works:

The GROUP BY statement groups the data in the table by the grouping column and creates a new result set with each group There is only one line. The aggregate value for each group is calculated based on the corresponding values ​​for that group in the original data.

Example:

Suppose we have a table named "sales" that contains the following data:

##Product ID##1Apple1002banana503Apple 754banana25 want To group sales by product name and calculate total sales for each product, you can use the following query:
<code class="sql">SELECT 产品名称, SUM(销售额) AS 总销售额
FROM sales
GROUP BY 产品名称;</code>
Copy after login
Product Name Sales

Result:

Product NameTotal SalesApple175banana75

The above is the detailed content of What is the statement used to group in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template