当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
给网站套上Cloudflare(以腾讯云为例)
IO进程线程->进程间的通信->day7
Flink Learning 12: DataStreaming API
线程池的使用(结合Future/Callable使用)
开启防火墙iptable规则后,系统网络变慢
MySQL表操作练习
Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
Tencent Business Security Post IDP Talk Summary
MAYA船的建模
2022杭电多校六 1007-Shinobu loves trip(同余方程)
RK3568环境安装
《PyTorch深度学习实践》第十一课(卷积神经网络CNN高级版)
After the firewall iptable rule is enabled, the system network becomes slow
Technical Analysis Patterns (11) How to Trade Head and Shoulders Patterns
(2022杭电多校六)1012-Loop(单调栈+思维)
二叉搜索树问题
软件测试必问面试题(附答案和解析)
不能比较或排序 text、ntext 和 image 数据类型
Database table insert data
对数据类型而言运算符无效。运算符为 add,类型为 text。