mah*_*esh 10 mysql sql database
下面是我的表
表格1
+--------+----------+---------+
| amount | make | product |
+--------+----------+---------+
| 100 | Nokia | Mobiles |
| 300 | Samesung | Mobiles |
| 700 | Micromax | Mobiles |
| 1000 | Karbonn | Mobiles |
| 500 | Lava | Mobiles |
| 100 | Floyer | Gift |
| 500 | Arichies | Gift |
| 300 | Feeling | Gift |
+--------+----------+---------+
Run Code Online (Sandbox Code Playgroud)
现在我想为每种产品显示两个最高金额...
所以我想构建单个SQL查询,它给出了如下结果.
+--------+----------+---------+
| amount | make | product |
+--------+----------+---------+
| 1000 | Karbonn | Mobiles |
| 700 | Micromax | Mobiles |
| 500 | Arichies | Gift |
| 300 | Feeling | Gift |
+--------+----------+---------+
Run Code Online (Sandbox Code Playgroud)
请帮我构建这样的查询..
Zan*_*ien 10
您可以使用此解决方案根据以下内容检索" group-wise maximum " amount:
SELECT a.*
FROM Table1 a
INNER JOIN Table1 b ON a.product = b.product AND a.amount <= b.amount
GROUP BY a.amount, a.product
HAVING COUNT(*) <= 2
Run Code Online (Sandbox Code Playgroud)
只需将每个产品要检索2的顶部行更改为多个.
如果要检索每个产品的最低两行,只需将<=符号更改INNER JOIN为a即可>=.
你可以在这里找到解决方案:SQL-Fiddle Demo
| 归档时间: |
|
| 查看次数: |
848 次 |
| 最近记录: |