[英]MySQL: SELECT First or Last Rows When Grouping By Multiple Columns
I have an SQL table contains data for the sale of some items. 我有一个SQL表,其中包含一些项目的销售数据。 In fact, it has the logs of the sale of items.
实际上,它具有项目销售的日志。
For example, there is a sale that contains 2 items: Keyboard ( id:1
) and mouse( id:2
). 例如,某笔交易包含2个项目:键盘(
id:1
)和鼠标( id:2
)。 Buyers can make bids to each item and multiple times, like ebay. 买家可以对每个项目进行多次出价,例如ebay。 So let's assume there are 2 buyers(
ids are 97 and 98
) made bids a couple of times. 因此,假设有2个买家(
ids are 97 and 98
)进行了几次出价。 The related data would be: 相关数据为:
bid_id | buyer_id | item_id | amount | time |
1 | 97 | 1 | 44.26 | 2014-01-20 15:53:16 |
2 | 98 | 2 | 30.47 | 2014-01-20 15:54:52 |
3 | 97 | 2 | 40.05 | 2014-01-20 15:57:47 |
4 | 97 | 1 | 42.46 | 2014-01-20 15:58:36 |
5 | 97 | 1 | 39.99 | 2014-01-20 16:01:13 |
6 | 97 | 2 | 24.68 | 2014-01-20 16:05:35 |
7 | 98 | 2 | 28 | 2014-01-20 16:08:42 |
8 | 98 | 2 | 26.75 | 2014-01-20 16:13:23 |
In this table, I need to select data for first item offers for each user and last offers for each user. 在此表中,我需要为每个用户的第一个商品报价和每个用户的最后一个报价选择数据。
So if I select first item offers for each user (distinct), return data should be like: 因此,如果我为每个用户(不同)选择第一项报价,则返回数据应类似于:
bid_id | buyer_id | item_id | amount | time |
1 | 97 | 1 | 44.26 | 2014-01-20 15:53:16 |
2 | 98 | 2 | 30.47 | 2014-01-20 15:54:52 |
3 | 97 | 2 | 40.05 | 2014-01-20 15:57:47 |
If I select last offers for each user, return should be like: 如果我为每个用户选择最后一个优惠,则退货应为:
bid_id | buyer_id | item_id | amount | time |
5 | 97 | 1 | 39.99 | 2014-01-20 16:01:13 |
6 | 97 | 2 | 24.68 | 2014-01-20 16:05:35 |
8 | 98 | 2 | 26.75 | 2014-01-20 16:13:23 |
Since I have to bring each item for each user, I tried to GROUP BY
for both buyer_id
and item_id
, then SELECT
the MIN
value of time
or bid_id
. 由于必须为每个用户携带每件商品,因此我尝试对
buyer_id
和item_id
都buyer_id
GROUP BY
,然后SELECT
time
或bid_id
的MIN
值。 But It always returned me first bid_id
but latest amount
rows (which are last offers actually). 但是,它总是向我返回第一个
bid_id
但返回最近的amount
行(实际上是最后报价)。
Here's the query I tried: 这是我尝试过的查询:
SELECT MIN(`bid_id`) AS `bid_id`,`buyer_id`,`item_id`,`amount`,`time` FROM `offers` GROUP BY `buyer_id`,`item_id`
And the result was: 结果是:
bid_id | buyer_id | item_id | amount | time |
1 | 97 | 1 | 39.99 | 2014-01-20 16:01:13 |
2 | 97 | 2 | 24.68 | 2014-01-20 16:05:35 |
3 | 98 | 2 | 26.75 | 2014-01-20 16:13:23 |
As you can see, it groups by and the IDs are correct but the rest of the row values are not. 如您所见,它的分组依据和ID是正确的,但其余的行值却不是。
How can I correctly SELECT
first and/or last rows when grouping buy multiple columns? 分组购买多列时如何正确
SELECT
第一行和/或最后一行?
SELECT o.`bid_id`,o.`buyer_id`,o.`item_id`,o.`amount`,o.`time` FROM `offers` o
JOIN
(SELECT MIN(`bid_id`) AS `bid_id`,`buyer_id`,`item_id`,`amount`,`time` FROM `offers` GROUP BY `buyer_id`,`item_id`)x
ON x.bid_id=o.bid_id AND x.buyer_id=o.buyer_id
Here's another take, using Quassnoi's ranking trick here 这是另一个使用Quassnoi的排名技巧的方法
For the first bids: 对于第一个出价:
SELECT x.bid_id, x.buyer_id, x.item_id, x.amount, x.time
FROM
(
SELECT o.bid_id, o.buyer_id, o.item_id, o.amount, o.time,
@combo :=CASE WHEN NOT(@curItem = o.item_id AND @curBuyer = o.buyer_id)
THEN 1 ELSE @combo+1 END AS Rank,
@curItem:=o.item_id AS item,
@curBuyer:=o.buyer_id AS buyer
FROM
(
SELECT o.bid_id, o.buyer_id, o.item_id, o.amount, o.time
FROM offers o
ORDER BY o.buyer_id, o.item_id, o.bid_id
) o,
(SELECT @curItem := -1) itm,
(SELECT @curBuyer:= -1) buy
) x
WHERE x.Rank = 1;
For the last bids query, you just need to change the ORDER BY
to o.buyer_id, o.item_id, o.bid_id DESC
对于最后一个出价查询,您只需将
ORDER BY
更改为o.buyer_id, o.item_id, o.bid_id DESC
first offers sql: 首先提供sql:
SELECT
*
FROM
offers AS o1
WHERE
NOT EXISTS (
SELECT
1
FROM
offers o2
WHERE
o1.buyer_id = o2.buyer_id
AND o1.item_id = o2.item_id
AND datetime(o1.time) > datetime(o2.time)
)
last offers sql : just change to datetime(o1.time) < datetime(o2.time) (i use sqlite~) 最后提供sql:只需更改为datetime(o1.time)<datetime(o2.time)(我使用sqlite〜)
Please try below query for the desired output. 请尝试在下面的查询中查询所需的输出。 SQL FIDDLE LINK : http://sqlfiddle.com/#!2/916c2/15
SQL链接: http ://sqlfiddle.com/#!2/916c2/15
(select f.bid_id,f.buyer_id,f.item_id,f.amount,f.time from offers f join
(select buyer_id,item_id,min(time) as time from offers
group by buyer_id,item_id)t
on f.buyer_id=t.buyer_id and f.item_id=t.item_id
and f.time=t.time)
union
(select f.bid_id,f.buyer_id,f.item_id,f.amount,f.time from offers f join
(select buyer_id,item_id,max(time) as time from offers
group by buyer_id,item_id)t
on f.buyer_id=t.buyer_id and f.item_id=t.item_id
and f.time=t.time);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.