首頁  >  文章  >  資料庫  >  mysql 排序依據

mysql 排序依據

黄舟
黄舟原創
2017-01-16 13:09:001324瀏覽

order by
排序:在結果集出來之後才有意義必須在where ,group by ,having 之後
desc(降序)/asc(升序)

用欄位排序 
排序選擇,先根據cat_id,然後shop_price

select goods_name,cat_id,shop_price from goods where cat_id=4 order by shop_price desc;

limit限制limit [pos, 可選偏移量] 數量 


升序排列取出前十名

select cat_id,shop_price,goods_name from goods order by cat_id ,shop_price;

價格最高的前五名
升序排列取出前十名

select goods_id,goods_name from goods where cat_id=3 order by shop_price asc limit 10;

價格最高的前五名
的從第三名開始的三名(或者說是第三名到第五名)

mysql> select goods_name ,shop_price from goods order by shop_price desc limit 0,5;

取出價格最高的商品

mysql> select goods_name ,shop_price from goods order by shop_price desc limit 5;
+----------------+------------+
| goods_name | shop_price |
+----------------+------------+
| 多普达Touch HD | 5999.00 | 
| 诺基亚N96 | 3700.00 | 
| 诺基亚N85 | 3010.00 | 
| testPhone | 3000.00 | 
| 夏新T5 | 2878.00 | 
+----------------+------------+

技巧:[判斷where] [分組group_by] [過濾having] [排序order by] [篩選limit]

取出每個類型中最新的產品

mysql> select goods_name ,shop_price from goods order by shop_price desc limit 2,3;
+------------+------------+
| goods_name | shop_price |
+------------+------------+
| 诺基亚N85 | 3010.00 | 
| testPhone | 3000.00 | 
| 夏新T5 | 2878.00 | 
+------------+------------+
mysql> select goods_name ,shop_price from goods order by shop_price desc limit 1;
+----------------+------------+
| goods_name | shop_price |
+----------------+------------+
| 多普达Touch HD | 5999.00 | 
+----------------+------------+

查詢出來的結果可以是

單列單行可以用= 再次過濾

單列多行可以用in 再次過濾
多列多行可以用from 再次過濾


以上就是mysql order by的內容,更多相關內容請關注PHP中文網(www.php.cn)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn