create table `shop` (
`id` int (10) PRIMARY KEY,
`shop_name` varchar (100),
`item_name` varchar (100),
`price` int (10)
);
每个shop 中价格最高的前N 条数据
select * from shop a where N > (select count(*) from shop b where b.shop_name = a.shop_name and b.price<a.price) order by a.shop_name,a.price desc;
每个shop 中价格最第的前N 条数据
select * from shop a where N > (select count(*) from shop b where b.shop_name = a.shop_name and b.price>a.price) order by a.shop_name,a.price desc;
这篇博客探讨了如何使用SQL查询来获取每个商店中价格最高的前N条记录以及价格最低的前N条记录。通过两个复杂的子查询,分别计算出每个商店内比当前商品价格高的商品数量和低的商品数量,从而筛选出所需的数据,并按商店名和价格进行排序。
155

被折叠的 条评论
为什么被折叠?



