gpt4 book ai didi

mysql - 每组最多 3 个结果(商店及其价格)

转载 作者:行者123 更新时间:2023-11-30 00:36:20 25 4
gpt4 key购买 nike

我必须从包含类似内容的表中获取

ShopId  ArticleId  Price  ArticleName

1 1a 2.00 Fahrrad
1 1b 3.00 Fahrrad
1 1c 4.00 Fahrrad
1 1d 9.99 Fahrrad
2 2a 2.00 Fahrrad
2 2b 3.10 Fahrrad
2 2c 3.50 Fahrrad
2 2d 9.99 Fahrrad

最大。每个 ShopId 按分钟订购 3 个 ArticleId。价格作为结果,例如:

ShopId  ArticleId  Price  ArticleName

1 1a 2.00 Fahrrad
1 1b 3.00 Fahrrad
1 1c 4.00 Fahrrad
2 2a 2.00 Fahrrad
2 2b 3.10 Fahrrad
2 2c 3.50 Fahrrad

有人可以帮忙吗?所以它类似于 min(Price) 和 group by ShopId 但有 3 个结果而不是 1 个。

最佳答案

给这个猛击一下。不知道您的表的名称,因此我将其命名为 shopArticle:

select t.ShopId,t.ArticleId,t.Price,t.ArticleName
from
(
select sa.*,
CASE sa.ShopId
WHEN @curShopId
THEN @curRow := @curRow + 1
ELSE @curRow := 1 AND @curShopId := sa.ShopId END rank
from shopArticle sa
join (SELECT @curRow := 0, @curShopId := '') r
order by sa.ShopId,sa.Price asc
) t
where t.rank <=3;

关于mysql - 每组最多 3 个结果(商店及其价格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com