一、order by
order by 是对最终结果集进行排序
order by应该在where/group by/having后面
单字段排序语法:order by 列名 desc/asc
desc是降序,asc是升序
多字段排序语法:order by 列名1 desc/asc,列名2 desc/asc;
二、limit
参数:limit offset N
offset:偏移量,跳过的行数,为0时可以省略
N:实际取出的行数
三、实际操作
1、取出第四个栏目下的商品,并按价格由高到低排序
select cat_id,goods_name,shop_price from goods where cat_id=4 order by shop_price desc;
2、取出第四个栏目下的商品,并且按发布时间降序排列
select cat_id,goods_name,goods_id from goods where cat_id=4 order by goods_id desc;
3、按栏目排序
扫描二维码关注公众号,回复:
9838812 查看本文章
select cat_id,goods_name from goods order by cat_id asc;
4、按栏目升序排列,按价格降序排列
select cat_id,goods_name,shop_price from goods order by cat_id asc,shop_price desc;
5、取出第3个栏目下按照价格从低到高排序的前10个商品
select cat_id,goods_name,shop_price from goods where cat_id=3 order by shop_price asc limit 10;
6、查询本店价格前三的商品
select goods_name,shop_price from goods order by shop_price desc limit 3;
7、查询本店价格最高的第三名到第五名的商品
select goods_name,shop_price from goods order by shop_price desc limit 2,3;