SQL - Order by Then Group By [英] SQL - Order by Then Group By

查看:32
本文介绍了SQL - Order by Then Group By的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示价格最便宜的商品,然后按字母顺序按商品分组.

I would like to show items with the cheapest price, then group by item alphabetically.

我在下面有一张图片最能描述我正在寻找的内容:

I have a picture below to best describes what I am looking for:

推荐答案

可以使用subquery:

select * 
from table t
where price = (select min(price) from table where Item = t.Item)
order by Item;

但是,当同一件商品有两个相同的最低价格时要小心

However, be careful while same item has two same min price

您可以更改where 子句并使用limit 子句

You can change the where clause and use limit clause

select * 
from table t
where id = (select id
            from table 
            where Item = t.Item
            order by price desc
            LIMIT 1)
order by Item;

这篇关于SQL - Order by Then Group By的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆