1. Overview Group by means to group data according to the rules after by. The so-called grouping means dividing the data set into several "small groups" and processing them accordingly. 2. Grammatical rules SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name 3. Examples We have an order table like this: We want to count the total amount of each user's order, we can use group by to achieve this: select Customer,sum(OrderPrice) as total_price group by Customer; The following result set appears: 4. Note If the following appears: select category, sum(quantity) as sum of quantity, summaryfrom Agroup by categoryorder by categorydesc An error will be reported because the selected field must either be included after group by as the basis for grouping, or be included in an aggregate function, otherwise the summary column cannot match a row. select category, sum(quantity) AS sum of quantities from Agroup by category order by sum(quantity) desc If you want to group by multiple columns, you can use the group by all syntax: select Customer,OrderDate,sum(OrderPrice) group by all Customer,OrderDate 5. The difference between where and having where is to remove rows that do not meet the where condition before query grouping, that is, filter data before grouping. The where condition cannot contain aggregate functions. select Customer,sum(OrderPrice) as total_price group by Customer having total_price>1700; The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Restart the Docker service to apply the automatic start and stop command (recommended)
>>: Teach you how to use vscode to build a react-native development environment
Table of contents Understanding Prototypes Unders...
1. Create a Docker network docker network create ...
As a super rookie, I just started learning MySQL ...
Table of contents Basic syntax for multi-table jo...
The following is a picture mouse hover zoom effec...
The first method: Use Junge's one-click scrip...
Install Jenkins via Yum 1. Installation # yum sou...
1.1 Building the Directory Structure This operati...
This article shares the specific code of the WeCh...
Some web pages may not look large but may be very...
Table of contents 1. Introduction to jQuery 2. jQ...
Modify /etc/my.cnf or /etc/mysql/my.cnf file [cli...
principle The principle of anti-shake is: you can...
Today, let's talk about how to use js to achi...
Table of contents Overview Subqueries Subquery Cl...