假设这是我的桌子
项目|姓名|价钱
----- ------------ --------
1 |文章310.00
2 |文章30.00
3 |提示150.00
4 |提示20.00
5 | NB | 20.00
我的查询
SELECT Item FROM Table WHERE NAME IN ('Article', 'Tips');
我如何才能为每个项目仅获得1/2行(文章和提示)?
假设这是我的桌子
项目|姓名|价钱
----- ------------ --------
1 |文章310.00
2 |文章30.00
3 |提示150.00
4 |提示20.00
5 | NB | 20.00
我的查询
SELECT Item FROM Table WHERE NAME IN ('Article', 'Tips');
我如何才能为每个项目仅获得1/2行(文章和提示)?
我认为您想使用“与众不同”修饰符,例如
or
SELECT distinct NAME, max(price) FROM Table WHERE NAME IN ('Article', 'Tips');
您可以使用窗口功能:
如果每个项目需要2行,则可以将“ 1”更改为“ 2”。
If you want a particular item -- the newest or oldest or longest name or most red, you can change the
ORDER BY
-- assuming you have columns to represent what you want.常见的解决方案是使用相关子查询进行过滤。例如,这为您提供了每个类别中最便宜的商品:
如果要随机行:
With
NOT EXISTS
:this query will return the rows with the minimum
item
.See the demo.
Results: