ホームページ  >  記事  >  データベース  >  mysqlの順序

mysqlの順序

黄舟
黄舟オリジナル
2017-01-16 13:09:001292ブラウズ

order by
Sort: 結果セットが出て初めて意味を持ちます。where、group by、
desc (降順)/asc (昇順) の後でなければなりません

フィールドの並べ替えを使用します

並べ替えには shop_price を使用します。降順

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

複数の並べ替え選択、最初はcat_id、次にshop_priceに基づいて

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

limit limit [pos、optional offset]数量

昇順に並べて上位10を取り出します

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

上位5つ一番高い値段で

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

一番高い値段 3位から3つ(または3位から5位まで)

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

ヒント: [どこで判断する] [グループ group_by] [ [フィルタの有無] [並べ替え順] [フィルタ制限]

各タイプの最新の商品を取得

select cat_id, goods_id ,goods_name from(
(select cat_id,goods_id ,goods_name from goods order by cat_id desc,goods_id desc ) as tmp
)group by cat_id order by cat_id desc;
select cat_id,goods_id ,goods_name from goods where goods_id in (
select max(goods_id) from goods group by cat_id 
) order by cat_id desc;

クエリの結果は次のようになります
単一列と単一行が使用可能 = 再度フィルタリング
単一列と複数行が可能もう一度フィルターで使用します
複数の列と複数の行を使用できますもう一度フィルターするには from を使用します

上記は mysql order by の内容です。その他の関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) に注目してください。 )!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:mysql null和「 」次の記事:mysql null和「 」