MySQL限制查詢和數(shù)據(jù)排序介紹
更新時間:2022年03月25日 09:52:30 作者:江下下啊
這篇文章主要介紹了MySQL限制查詢和數(shù)據(jù)排序介紹,通過limit可以限制返回結(jié)果的行數(shù),而當數(shù)據(jù)查詢出來以后,我們可以對數(shù)據(jù)進行排序處理。在末尾使用order?by語句,下文相關(guān)詳細介紹,需要的小伙伴可以參考一下
一、限制查詢
我們通過limit可以限制返回結(jié)果的行數(shù)
select * from 表名 limit count;
select * from users limit 3;

1.指定從第幾行起,返回多少行
select * from 表名 limit start,count;
select * from users limit 2,3;
相等
select * from users limit 3 offset 2;

2.取最大值
select * from users order by age desc limit 1;

3.取最小值
select * from users order by age asc limit 1;

4.分頁
select * from users limit (page-1)*pageSize,pageSize;

二、數(shù)據(jù)排序
當數(shù)據(jù)查詢出來以后,我們可以對數(shù)據(jù)進行排序處理。在末尾使用order by語句。
select * from 表名 order by 列1 asc|desc,列2 asc|desc,...
注:
- asc即為升序,也是默認值。
- desc即為降序
- 排序首先先按照列1進行排序,如果出現(xiàn)結(jié)果相同的,在進行列2排序
1.年齡大于10的根據(jù)id進行降序排序
select * from users where age > 10 order by id desc;

2.年齡大于10的按照id進行升序排序
select * from users where age > 10 order by id asc;

到此這篇關(guān)于MySQL限制查詢和數(shù)據(jù)排序介紹的文章就介紹到這了,更多相關(guān)MySQL限制查詢數(shù)據(jù)排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL數(shù)據(jù)庫自增主鍵的間隔不為1的解決方式
這篇文章主要介紹了MySQL數(shù)據(jù)庫自增主鍵的間隔不為1的解決方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Windows10下mysql 8.0.22 安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了Windows10下mysql 8.0.22 安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11

