当前位置:网站首页>MySQL: order by sorting query, group by grouping query
MySQL: order by sorting query, group by grouping query
2022-08-05 07:11:00 【_Sauron】
Article table of contents
order by query
That is, the query is performed in ascending or descending order according to the field. The default is ascending order, which can be specified according to the asc and desc suffixes.
Example:
Source data
Sort by age:
select* from user order by age;

Sort by name: will be sorted lexicographically


Specify multiple fields:
The meaning of the following statement is that if the names are the same, the age is sorted in ascending order

Use with where:

group by group query
When querying, if you want to know how old the user's age is in the table, you can normally use deduplication to query:

This can also be achieved using group by:

But if you want to know how many people there are in each age group, there is no way to deduplicate, but group by is still possible:

You can also remove unwanted data after grouping:

You can group by multiple fields:

Can be combined with order by query:

Performance issues
Actually, the queries of limit, order by, group by are all related to the index. In this table, the primary key is id, and other fields have no indexes, so when operating on other fields such as age, you can seeThe last column shows that the query needs to use filesort, that is, file sorting, which involves a lot of operations on the disk; and the index used to query the id is directly queried using the index.

边栏推荐
- C# FileSystemWatcher
- (2022杭电多校六)1012-Loop(单调栈+思维)
- It turns out that Maya Arnold can also render high-quality works!Awesome Tips
- typescript60-泛型工具类型(readonly)
- [instancetype type Objective-C]
- RNote108---Display the running progress of the R program
- protobuf根据有关联的.proto文件进行编译
- 性能提升400倍丨外汇掉期估值计算优化案例
- 技术分析模式(七)发挥差距
- 【Go】IM系统Centrifugo
猜你喜欢
随机推荐
技术分析模式(八)双顶和底
【LeetCode】235.二叉搜索树的最近公共祖先
typescript67-索引查询类型
Modeling of the MAYA ship
利用将网页项目部署到阿里云上(ngnix)
protobuf is compiled against the associated .proto file
基于KECA-IGWO-KELM的间歇过程故障诊断方法
[Tool Configuration] Summary of Common Uses of VSCode
《PyTorch深度学习实践》第十课(卷积神经网络CNN)
MySQL:连接查询 | 内连接,外连接
Shiny04---DT和进度条在shiny中的应用
Flink Learning 12: DataStreaming API
2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams
Redis
ndk编译so库
AI+视频技术助力保障校园安全,校园智能安防平台该如何建设?
Task flow scheduling tool AirFlow,, 220804,,
Technical Analysis Mode (8) Double Top and Bottom
MySQL:JDBC编程
IO进程线程->进程间的通信->day7









