mysql学习--order by 与limit

一、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、按栏目排序

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;

在这里插入图片描述

发布了20 篇原创文章 · 获赞 0 · 访问量 228

猜你喜欢

转载自blog.csdn.net/alyssa_yu/article/details/104794274