-1

我想知道是否可以通过查询来完成此操作:

我有一张针对不同产品的出价表

bid_id, product_id, date, price

bid_id 是递增键,product_id 是来自另一个产品详细信息表的外键。

我想选择所有提高产品出价的出价。以我在这张桌子上的东西,可以做到吗?

让我试着让我自己更清楚:对于每一种产品,我都想获得所有从之前的出价中提高价格的 dib。

例如:如果表包含

1, 12202, "2013-09-08 13:41:45", 120
2, 17394, "2013-09-08 13:43:32", 250
3, 12202, "2013-09-08 13:54:11", 170
4, 12202, "2013-09-08 14:05:37", 210

我想将第 3 行和第 4 行作为输出。(一种产品可以有多条生产线)

谢谢!

4

1 回答 1

6

通过“提高价格”,我认为您的意思是出价高于该产品之前的任何出价。

如果是这样,答案是“是”。可以办到。

哦,您可能也想查看查询。

select b.*
from (select b.*,
             (select max(b2.price)
              from bids b2
              where b2.product_id = b.product_id and
                    b2.bid_id < b.bid_id
             ) as prevmaxprice
      from bids b
     ) b
where b.price > prevmaxprice;

编辑:

如果要从之前的出价提高价格,查询类似,但子查询逻辑有点不同:

select b.*
from (select b.*,
             (select b2.price
              from bids b2
              where b2.product_id = b.product_id and
                    b2.bid_id < b.bid_id
              order by b2.bid_id desc
              limit 1
             ) as prevprice
      from bids b
     ) b
where b.price > prevprice;
于 2013-09-09T19:17:42.530 回答