当前位置:网站首页>MySQL data query (subtotal and sorting)
MySQL data query (subtotal and sorting)
2022-07-30 03:59:00 【Good luck.112】
1, GROUP BY clause
The GROUP BY clause is primarily used to group rows based on fields.For example, all rows in the student base table are grouped according to the major the student is studying, resulting in a group of students for each major.
Syntax format: GROUP BY[column name] [ ASC | DESC ],...[ WITH ROLLUP ]
GROUP BY can be used to group by one or more columns or by expressions, and is often used with aggregate functions.
Example: output the book category name in the Book table
SELECT Book CategoryFROM BookGROUP BY book category;
Example: Count the inventory of various books in the Book table by book category
SELECT book category,COUNT(*)AS 'stock number'FROM BookGROUP BY book category;
Use the GROUP BY clause with the ROLLUP operator to specify that the result set does not contain normal rows provided by GROUP BY, but also includes summary rows.
Example: Count the inventory of various books in the Book table by book category and publisher, and make a subtotal by category
SELECT book category, publisher, Sum (quantity) AS 'stock number'FROM BookGROUP BY Book Category, PublisherWITH ROLLUP;
2. HAVING clause
The purpose of using the HAVING clause is similar to the WHERE clause, except that the WHERE clause is used to select rows after the FROM clause, and the HAVING clause is used to select rows after the GROUP BY clause.
Syntax format: HAVING condition
The SQL standard requires that HAVING must refer to columns in the GROUP BY clause or columns used in aggregate functions.However, MySQL supports extensions to the nature of this work and allows HAVING to reference columns in the SELECT list and columns in outer subqueries.
Example: Find the ID number and average number of books ordered by each member in the Sell table whose average number of books is more than 10.
SELECT ID number, AVG (number of books ordered) AS 'average number of books ordered'FROM SellGROUP BY ID numberHAVING AVG(Number of copies)>10;
3. ORDER BY clause
In a SELECT statement, if the ORDER BY clause is not used, the order of the rows in the result is unpredictable.Using the ORDER BY clause ensures that the rows in the result are in a certain order.
Syntax format: ORDER BY{column name | expression | column number}[ ASC | DESC ],...
The keyword ASC means ascending order, DESC means descending order, the system default value is ASC
Example: Sort the records in the Book table by publication time
SELECT *FROM BookORDER BY Publication date;
Example: Arrange the records in the Sell table from high to bottom according to the number of books ordered
SELECT *FROM SellORDER BY DESC;
4. LIMIT clause
The LIMIT clause is the last clause of the SELECT statement and is mainly used to limit the number of rows returned by the SELECT statement
Syntax format: LIMIT{[offset,]lines|linesOFFSEToffset}
For example, LIMIT 5 means returning the first 5 rows in the result set of the SELECT statement, while LIMIT 3,5 means returning 5 rows starting from the fourth row.
Example: Find the information of the top 5 registered members in the Members table
SELECT * FROM MembersORDER BY registration timeLIMIT 5;
When the initialization is not from scratch, use two parameters LIMIT offset, the number of lines, it is worth noting that The offset of the initial line is 0 instead of 1.
Example: Find 5 records starting from the 4th record in the Book table
SELECT *FROM BookORDER BY student numberLIMIT 3,5;
边栏推荐
- WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template
- Boutique: Taobao/Tmall Get Order Details API for Purchased Products
- 一起来学习flutter 的布局组件
- Pytorch框架学习记录2——TensorBoard的使用
- Rpc 和 gRpc 简介汇总
- Smart answer function, CRMEB knowledge payment system must have!
- Redis "super explanation!!!!!!"
- Advanced Microservices Cloud Alibaba
- 操作配置:如何在一台服务器中以服务方式运行多个EasyCVR程序?
- redis分布式锁的原子保证
猜你喜欢
(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (5) Task Book
Pytorch框架学习记录7——卷积层
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (2) Mini Program Function
发给你的好友,让 TA 请你吃炸鸡!
What is the difference between mission, vision and values?
Redis【超详解!!!】
第51篇-知乎请求头参数分析【2022-07-28】
逆向理论知识3【UI修改篇】
Pytorch framework learning record 7 - convolutional layer
随机推荐
运行时间监控:如何确保网络设备运行时间
spicy (1) basic definition
高并发框架 Disruptor
小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书
Tcp programming
mysql 结构、索引详解
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (2) Mini Program Function
sublime text 3 settings
小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (5) Task Book
SDL player in action
小程序毕设作品之微信二手交易小程序毕业设计成品(3)后台功能
spicy(二)unit hooks
spicy(一)基本定义
The first day of Flink learning - what is batch and streaming computing?
Roperties class configuration file & DOS to view the host network situation
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report
LoadBalancer load balancing
Chapter 51 - Knowing the request header parameter analysis【2022-07-28】
redis分布式锁的原子保证