国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

MySQL 查詢的排序、分頁相關

瀏覽:123日期:2023-10-09 11:13:15

概述

數據庫中的數據直接呈現出來一般不是我們想要的,所以我們上兩節演示了如何對數據進行過濾的方法。除了對數據進行過濾,

我們可能還需要對數據進行排序,比如想從列表中了解消費最高的項,就可能需要對金額字段做降序排序,想看年齡從小到大的分布情況,就可能需要對user表的age字段進行升序排序。

也可能需要對數據進行限制,比如我們需要對付款的1~10,11~20,21~30 名的用戶分別贈予不同的禮品,這時候對數據的限制就很有用了。

備注:下面腳本中[]包含的表示可選,| 分隔符表示可選其一。

數據排序 order by

語法格式如下:

1、需要排序的字段跟在order by之后;

2、asc 和 desc表示排序的規則,asc:升序,desc:降序,默認為升序 asc;

3、排序可以指定多次字段,多字段排序之間用逗號隔開。

4、多字段排序中,越靠前優先級越高,下面中cname1優先排序,當cname1等值的時候,cname2開始排序,直至所有字段都排序完。

select cname from tname order by cname1 [asc|desc],cname2 [asc|desc]...;

單個字段排序

舉個例子,在銷售額中通按照交易的訂單進行金額額度降序的方式顯示:

mysql> select * from t_order;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 8 | brand | 52.2 | 2 || 9 | hen | 1752.02 | 7 || 10 | helyn | 88.5 | 4 || 11 | sol | 1007.9 | 11 || 12 | diny | 12 | 1 || 13 | weng | 52.2 | 5 || 14 | sally | 99.71 | 9 |+---------+---------+---------+-------+7 rows in setmysql> select * from t_order order by amount desc;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 9 | hen | 1752.02 | 7 || 11 | sol | 1007.9 | 11 || 14 | sally | 99.71 | 9 || 10 | helyn | 88.5 | 4 || 8 | brand | 52.2 | 2 || 13 | weng | 52.2 | 5 || 12 | diny | 12 | 1 |+---------+---------+---------+-------+7 rows in set

多個字段排序

多個字段排序用逗號隔開,優先級從左到右逐次遞減,如下圖,如果金額一致,則按照購買商品數量從多到少排序:

mysql> select * from t_order order by amount desc,goods desc;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 9 | hen | 1752.02 | 7 || 11 | sol | 1007.9 | 11 || 14 | sally | 99.71 | 9 || 10 | helyn | 88.5 | 4 || 13 | weng | 52.2 | 5 || 8 | brand | 52.2 | 2 || 12 | diny | 12 | 1 |+---------+---------+---------+-------+7 rows in set

按alias排序

按照別名排序或者做條件查詢的目的都是為了簡化代碼,方便使用,別名可以是英文,也可以是中文:

mysql> select account as ac,amount as am,goods as gd from t_order order by am,gd desc;+-------+---------+----+| ac | am | gd |+-------+---------+----+| diny | 12 | 1 || weng | 52.2 | 5 || brand | 52.2 | 2 || helyn | 88.5 | 4 || sally | 99.71 | 9 || sol | 1007.9 | 11 || hen | 1752.02 | 7 |+-------+---------+----+7 rows in set

字段排序中使用函數

下面使用了abs取絕對值函數,所以在 am字段降序排序中,-99.99 排在 99.71之上。

mysql> select * from t_order;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 8 | brand | 52.2 | 2 || 9 | hen | 1752.02 | 7 || 10 | helyn | 88.5 | 4 || 11 | sol | 1007.9 | 11 || 12 | diny | 12 | 1 || 13 | weng | 52.2 | 5 || 14 | sally | 99.71 | 9 || 15 | brand1 | -99.99 | 5 |+---------+---------+---------+-------+8 rows in setmysql> select account as ac,amount as am,goods as gd from t_order order by abs(am) desc;+--------+---------+----+| ac | am | gd |+--------+---------+----+| hen | 1752.02 | 7 || sol | 1007.9 | 11 || brand1 | -99.99 | 5 || sally | 99.71 | 9 || helyn | 88.5 | 4 || brand | 52.2 | 2 || weng | 52.2 | 5 || diny | 12 | 1 |+--------+---------+----+8 rows in set

與Where條件結合使用

order 在 where 條件之后,根據where已經過濾好的數據再進行排序。下面是過濾出購買金額>80 且 購買數量>5的數據,并且按照價格降序排序。

mysql> select * from t_order;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 8 | brand | 52.2 | 2 || 9 | hen | 1752.02 | 7 || 10 | helyn | 88.5 | 4 || 11 | sol | 1007.9 | 11 || 12 | diny | 12 | 1 || 13 | weng | 52.2 | 5 || 14 | sally | 99.71 | 9 || 15 | brand1 | -99.99 | 5 |+---------+---------+---------+-------+8 rows in setmysql> select * from t_order where amount>80 and goods>5 order by amount desc;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 9 | hen | 1752.02 | 7 || 11 | sol | 1007.9 | 11 || 14 | sally | 99.71 | 9 |+---------+---------+---------+-------+

數據limit

很多時候我們過濾出符合要求的數據之后,還需要得到這些數據中的某一個具體區間,比如對付款超過1000的用戶的第1~10,11~20,21~30 名分別贈予不同的禮品,這時候就要使用limit操作了。

limit用來限制select查詢返回的數據,常用于數據排行或者分頁等情況。

語法格式如下:

select cname from tname limit [offset,] count;

1、offset表示偏移量,就是指跳過的行數,可以省略不寫,默認為0,表示跳過0行,如 limit 8 等同于 limit 0,8。

2、count:跳過偏移量offset之后開始取的數據行數,有count行。

3、limit中offset和count的值不能用表達式。

獲取前n條記錄

如下圖,limit n 和 limit 0,n 是一致的:

mysql> select * from t_order;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 8 | brand | 52.2 | 2 || 9 | hen | 1752.02 | 7 || 10 | helyn | 88.5 | 4 || 11 | sol | 1007.9 | 11 || 12 | diny | 12 | 1 || 13 | weng | 52.2 | 5 || 14 | sally | 99.71 | 9 || 15 | brand1 | -99.99 | 5 |+---------+---------+---------+-------+8 rows in setmysql> select * from t_order limit 2;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 8 | brand | 52.2 | 2 || 9 | hen | 1752.02 | 7 |+---------+---------+---------+-------+2 rows in setmysql> select * from t_order limit 0,2;+---------+---------+---------+-------+| orderid | account | amount | goods |+---------+---------+---------+-------+| 8 | brand | 52.2 | 2 || 9 | hen | 1752.02 | 7 |+---------+---------+---------+-------+2 rows in set

limit限制單條記錄

這邊我們獲取支付金額中最大和最小的的一條記錄??梢韵仁褂?order 條件進行排序,然后limit 第1條記錄即可:

mysql> select * from t_order; +---------+---------+---------+-------+ | orderid | account | amount | goods | +---------+---------+---------+-------+ | 8 | brand | 52.2 | 2 | | 9 | hen | 1752.02 | 7 | | 10 | helyn | 88.5 | 4 | | 11 | sol | 1007.9 | 11 | | 12 | diny | 12 | 1 | | 13 | weng | 52.2 | 5 | | 14 | sally | 99.71 | 9 | | 15 | brand1 | -99.99 | 5 | +---------+---------+---------+-------+ 8 rows in set mysql> select * from t_order where amount>0 order by amount desc limit 1; +---------+---------+---------+-------+ | orderid | account | amount | goods | +---------+---------+---------+-------+ | 9 | hen | 1752.02 | 7 | +---------+---------+---------+-------+ 1 row in set mysql> select * from t_order where amount>0 order by amount asc limit 1; +---------+---------+--------+-------+ | orderid | account | amount | goods | +---------+---------+--------+-------+ | 12 | diny | 12 | 1 | +---------+---------+--------+-------+ 1 row in set

以上就是MySQL 查詢的排序、分頁相關的詳細內容,更多關于MySQL 查詢的資料請關注好吧啦網其它相關文章!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 一区二区三区在线 | 欧 | 欧美自拍视频 | 操欧美女 | 亚洲欧美综合一区二区三区四区 | 久久99国产精品免费观看 | 欧美特级大片 | 成人毛片手机版免费看 | 成人a视频片在线观看免费 成人a视频在线观看 | 精品久久久久久综合日本 | 女仆色在线 | 中文字幕亚洲一区二区va在线 | 1024手机基地在线看手机 | 91视频社区 | 久久久久88色偷偷免费 | 久久久久久久久影院 | 成人国产精品一级毛片了 | 国产在线视频精品视频免费看 | 欧美国产三级 | 国产视频软件在线 | 九九干| 97青草香蕉依人在线播放 | 欧美成人精品高清在线观看 | www.黄色com| 日韩久久久精品首页 | 91大神在线精品视频一区 | 日韩一区视频在线 | a级片在线免费看 | a级黄色毛片免费播放视频 a级精品九九九大片免费看 | 一级片网址 | 一级毛片免费观看不卡视频 | 夜夜爽影院| 久草免费在线观看 | 欧美在线二区 | 日本天堂网在线 | 欧美日韩高清不卡免费观看 | 欧美另类孕交免费观看 | 成人在线视频免费 | 久久91av| 亚洲自拍中文 | 中国性猛交xxxxx免费看 | 91小视频在线观看免费版高清 |