MySQL:按多列分组时选择第一行或最后一行

时间:2014-01-21 14:47:49

标签: mysql sql select group-by distinct

我有一个SQL表包含销售某些项目的数据。事实上,它有物品销售的日志。

例如,有一个包含2个项目的促销:键盘(id:1)和鼠标(id:2)。买家可以多次对每件商品进行出价,比如ebay。因此,我们假设有2位买家(ids are 97 and 98)多次出价。相关数据将是:

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 |

在此表中,我需要为每个用户选择第一项商品的数据,并为每个用户选择最后的商品。

因此,如果我为每个用户选择第一项商品(不同),则返回数据应为:

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 |

如果我为每个用户选择最后一个优惠,则返回应该是:

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 |

由于我必须为每个用户提供每个项目,因此我尝试GROUP BYbuyer_id item_id,然后SELECT MIN的{​​{1}}值timebid_id。但它始终首先返回bid_id但最新的amount行(实际上是最后一次提供)。

这是我试过的查询:

SELECT MIN(`bid_id`) AS `bid_id`,`buyer_id`,`item_id`,`amount`,`time` FROM `offers` GROUP BY `buyer_id`,`item_id`

结果是:

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 |

如您所见,它分组依据且ID正确但其余行值不正确。

分组购买多列时,如何正确SELECT第一行和/或最后一行?

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

这是另一种观点,使用Quassnoi's ranking trick here

首次出价:

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;

对于上次出价查询,您只需将ORDER BY更改为o.buyer_id, o.item_id, o.bid_id DESC

SqlFiddle here

答案 2 :(得分:0)

首先提供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 offer sql:只需更改为datetime(o1.time)< datetime(o2.time)(我使用sqlite~)

答案 3 :(得分:-1)

请在下面查询所需的输出。 SQL FIDDLE LINK: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);