选择多个最大值
我有一个名为 order
的表,其中包含 id
、user_id
、price
和 item_id
列>。商品价格不固定,我想选择每件商品最贵的订单。我想在同一查询中选择 user_id
、item_id
和 price
。我尝试了以下查询,但它没有返回正确的结果集。
SELECT user_id, item_id, MAX(price)
FROM order
GROUP BY item_id
此查询返回的某些行的 user_id
不正确。但是,结果集中的所有行都显示每个商品的正确最高价格。
I have a table called order
which contains columns id
, user_id
, price
and item_id
. Item prices aren't fixed and I would like to select each item's most expensive order. I want to select user_id
, item_id
and price
in the same query. I tried the following query but it doesn't return the correct result set.
SELECT user_id, item_id, MAX(price)
FROM order
GROUP BY item_id
Some of the rows returned by this query have the wrong user_id
. However, all rows in the result set show each item's correct highest price.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可能想要使用派生表,如下所示:
测试用例:
结果:
You may want to use a derived table, as follows:
Test case:
Result:
也许这有点长,但你会提高可读性
Maybe this is a little longer but you gain in readability
您需要首先获取每个商品 id 的最高价格,然后返回到
order
以获取以最高价格订购该商品的记录。像下面这样的查询应该可以工作。尽管如此,它会返回具有最高商品价格的所有记录。You need to first get the maximum price for each item id and then join back to
order
to get records where the item was ordered for the maximum price. Something like the following query should work. Although, it will return all records with the maximum item prices.这是一个每组最大问题。对于这个常见问题,有各种方法。在 MySQL 上,使用空自连接通常比任何涉及子查询的方法更快、更简单:
即。 “选择价格较高的同一商品不存在其他行的每一行”。
(如果两行具有相同的最高价格,您将同时返回。在平局的情况下到底该怎么做是每组最大解决方案的普遍问题。)
This is a per-group-maximum question. There are various approaches to this common problem. On MySQL it's typically faster and simpler to use a null-self-join than anything involving subqueries:
ie. “select each row where there exists no other row for the same item with a higher price”.
(If two rows have the same maximum price you will get both returned. What exactly to do in the case of a tie is a general problem for per-group-maximum solutions.)
您使用的 SQL 与 GROUP 相矛盾。
一旦使用GROUP,MYSQL总是会选择第一个user_id,但是最高的价格,这就是为什么用户错了但价格对的原因。
您可以尝试添加
ORDER BY Price DESC
来看看会发生什么,但我没有在我的环境中尝试。The SQL you used is contradictory by GROUP.
Once you use GROUP, MYSQL will always select the FIRST user_id, but the HIGHEST price, this is the reason why the user is wrong but the price is right.
You can try to add
ORDER BY price DESC
to see what happen, but I did not try in my environment.您的查询按
item_id
对行进行分组。如果您有多个item_id
1 的项目,且具有不同的user_id
,则只会选择第一个user_id
,而不是user_id< /code> 价格最高。
Your query groups the rows by
item_id
. If you have multiple items withitem_id
1, with different auser_id
, it will only pick the firstuser_id
, not theuser_id
with the highest price.您需要按 item_id 和 user_id 进行分组(显示每个用户每件商品的最高价格),或者如果您只想要组中的商品,则需要重新考虑 user_id 列。
例如,显示商品的最高价格并显示最后更改价格的用户,或者显示商品的最高价格并显示制定该商品最高价格的用户等。请查看 这篇文章的一些模式这。
You'll either need to group by item_id AND user_id (showing the max price per item per user), or if you want just the item in the group you'll need to rethink the user_id column.
e.g. show the max price for an item and show the LAST user who made a change on the price, OR show the Max price for an item and show the user who MADE the Max Price for the item etc. Have a look at this post for some patterns for doing this.
如果您想要订单中的前 2 个,请尝试此...
如果您想要前 3 个,则只需更改最后一个条件
其中
item_rank in (1,2) ;
到where item_rank in (1,2,3) ;
if you want top 2 from order try this ...
if you want top 3 then just change last condition
where
item_rank in (1,2) ;
towhere item_rank in (1,2,3) ;