ZBLOG

mysql GROUP BY

The GROUP BY clause in MySQL is used to group rows that have the same values into summary rows, like “find the sum of sales for each product”. It works in conjunction with aggregate functions such as SUM, COUNT, AVG, etc.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s);

Example: Let’s say we have a table named “sales” containing data about sales made by different salespeople in different cities.

salesperson city sale_amount
John New York 500
Anna London 700
Mary Paris 800
John New York 300
Anna Paris 400
Mary Paris 600

If we want to find the total sales amount for each salesperson, we can use the following query:

SELECT salesperson, SUM(sale_amount) AS total_sales
FROM sales
GROUP BY salesperson;

This will give us the result:

| salesperson | total_sales |
|-------------|-------------|
| Anna        | 1100        |
| John        | 800         |
| Mary        | 1400        |

The query groups the rows by “salesperson”, calculates the sum of “sale_amount” for each group, and returns the results in a summary table.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=486

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?