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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MySQL 查詢(xún)的排序、分頁(yè)相關(guān)

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

概述

數(shù)據(jù)庫(kù)中的數(shù)據(jù)直接呈現(xiàn)出來(lái)一般不是我們想要的,所以我們上兩節(jié)演示了如何對(duì)數(shù)據(jù)進(jìn)行過(guò)濾的方法。除了對(duì)數(shù)據(jù)進(jìn)行過(guò)濾,

我們可能還需要對(duì)數(shù)據(jù)進(jìn)行排序,比如想從列表中了解消費(fèi)最高的項(xiàng),就可能需要對(duì)金額字段做降序排序,想看年齡從小到大的分布情況,就可能需要對(duì)user表的age字段進(jìn)行升序排序。

也可能需要對(duì)數(shù)據(jù)進(jìn)行限制,比如我們需要對(duì)付款的1~10,11~20,21~30 名的用戶分別贈(zèng)予不同的禮品,這時(shí)候?qū)?shù)據(jù)的限制就很有用了。

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

數(shù)據(jù)排序 order by

語(yǔ)法格式如下:

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

2、asc 和 desc表示排序的規(guī)則,asc:升序,desc:降序,默認(rèn)為升序 asc;

3、排序可以指定多次字段,多字段排序之間用逗號(hào)隔開(kāi)。

4、多字段排序中,越靠前優(yōu)先級(jí)越高,下面中cname1優(yōu)先排序,當(dāng)cname1等值的時(shí)候,cname2開(kāi)始排序,直至所有字段都排序完。

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

單個(gè)字段排序

舉個(gè)例子,在銷(xiāo)售額中通按照交易的訂單進(jì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 |+---------+---------+---------+-------+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

多個(gè)字段排序

多個(gè)字段排序用逗號(hào)隔開(kāi),優(yōu)先級(jí)從左到右逐次遞減,如下圖,如果金額一致,則按照購(gòu)買(mǎi)商品數(shù)量從多到少排序:

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排序

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

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

字段排序中使用函數(shù)

下面使用了abs取絕對(duì)值函數(shù),所以在 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條件結(jié)合使用

order 在 where 條件之后,根據(jù)where已經(jīng)過(guò)濾好的數(shù)據(jù)再進(jìn)行排序。下面是過(guò)濾出購(gòu)買(mǎi)金額>80 且 購(gòu)買(mǎi)數(shù)量>5的數(shù)據(jù),并且按照價(jià)格降序排序。

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 |+---------+---------+---------+-------+

數(shù)據(jù)limit

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

limit用來(lái)限制select查詢(xún)返回的數(shù)據(jù),常用于數(shù)據(jù)排行或者分頁(yè)等情況。

語(yǔ)法格式如下:

select cname from tname limit [offset,] count;

1、offset表示偏移量,就是指跳過(guò)的行數(shù),可以省略不寫(xiě),默認(rèn)為0,表示跳過(guò)0行,如 limit 8 等同于 limit 0,8。

2、count:跳過(guò)偏移量offset之后開(kāi)始取的數(shù)據(jù)行數(shù),有count行。

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

獲取前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 條件進(jìn)行排序,然后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 查詢(xún)的排序、分頁(yè)相關(guān)的詳細(xì)內(nèi)容,更多關(guān)于MySQL 查詢(xún)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 久草青青 | 欧美日韩在线视频 | 亚洲国产精品欧美日韩一区二区 | 成年女人午夜免费视频 | 成人欧美一区二区三区黑人 | 日韩午夜精品 | 国产精品久久免费视频 | 狠狠色丁香婷婷综合 | 亚洲综合影视 | xxxx肥婆性bbbb欧美 | 成人午夜免费观看 | 中文成人在线 | 精品99视频 | 在线亚洲精品国产波多野结衣 | aaa成人永久在线观看视频 | 久草在线免费资源 | 三级毛片免费看 | 一级aaaaaa毛片免费 | 久久久美女视频 | 欧美日韩国产高清一区二区三区 | 欧美不卡一区二区三区 | 免费特黄一级欧美大片在线看 | 欧日韩视频 | 久久国产精品自线拍免费 | 一区二区视频在线 | 一a一级片 | 最新更新国内自拍视频 | 亚洲精品视频网 | 91精选视频在线观看 | 亚洲精品国产精品国自产网站 | 国产精品亚洲专区在线播放 | 国产亚洲欧美精品久久久 | 亚洲性在线观看 | 成视频年人黄网站免费 | 三级带黄色 | 国产美女做爰免费视频网址 | 久久视频免费 | 在线观看国产精成人品 | 玖草视频在线 | 久久99综合国产精品亚洲首页 | 亚洲成人一区二区 |