当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
ndk编译so库
Hash 这些知识你也应该知道
不太会讲爱,其实已经偷偷幸福很久啦----我们的故事
typescript67-索引查询类型
Day9 of Hegong Daqiong team vision team training - camera calibration
《PyTorch深度学习实践》第十课(卷积神经网络CNN)
技术分析模式(七)发挥差距
Falsely bamboo brother today and found a localization of API to use tools
【Go】IM系统Centrifugo
Using printf function in STM32
【instancetype类型 Objective-C】
typescript64-映射类型
Kioxia and Aerospike Collaborate to Improve Database Application Performance
共享内存+inotify机制实现多进程低延迟数据共享
DNSlog外带数据注入
RNote108---Display the running progress of the R program
C# FileSystemWatcher
怎么样避免线上内存泄漏
2022 crane driver (limited bridge crane) exam question bank and simulation test
typescript63-索引签名类型