【问题标题】:mysql correlated subquery : select field1 where max(field2)mysql 相关子查询:选择 field1 where max(field2)
【发布时间】:2015-01-11 15:31:52
【问题描述】:

选择价格最高上涨时间的最有效方法是什么? [底部结构]

-- 获得最高涨价

select p1.pricetime, max(p2.price) maxnext
from prices p1 inner join prices p2 on p2.id > p1.id
group by p1.pricetime

什么是 p2.pricetime,其中每个 p1.pricetime 的 p2.price = max(p2.price)?

-- 获取最高价格的时间

select p3.pricetime, x.maxnext
from prices p3 inner join 

(select p1.pricetime, max(p2.price) maxnext
from prices p1 inner join prices p2 on p2.id > p1.id
group by p1.pricetime) x

on x.maxnext = p3.price and p3.id > p1.id

对于数百万行表来说,这是一种非常低效的方法 我相信你可以在 MSSQL 中做这样的事情:

select p2.pricetime from 
(select p1.pricetime, max(p2.price) maxnext
from prices p1 inner join prices p2 on p2.id > p1.id
group by p1.pricetime) x ...

哪个从子查询外部访问子查询别名?

--结构:

CREATE TABLE `prices` (
  `id` int(11) NOT NULL DEFAULT '0',
  `pricetime` varchar(19) DEFAULT NULL,
  `price` decimal(10,8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

LOCK TABLES `prices` WRITE;
/*!40000 ALTER TABLE `prices` DISABLE KEYS */;

INSERT INTO `prices` (`id`, `pricetime`, `price`)
VALUES
    (1,'2014-01-01 21:55:00',1.37622000),
    (2,'2014-01-01 21:56:00',1.37616000),
    (3,'2014-01-01 21:57:00',1.37616000),
    (4,'2014-01-01 21:58:00',1.37498000),
    (5,'2014-01-01 21:59:00',1.37529000),
    (6,'2014-01-01 22:03:00',1.37518000),
    (7,'2014-01-01 22:05:00',1.37542000),
    (8,'2014-01-01 22:06:00',1.37558000),
    (9,'2014-01-01 22:07:00',1.37560000),
    (10,'2014-01-01 22:08:00',1.37560000);

/*!40000 ALTER TABLE `prices` ENABLE KEYS */;
UNLOCK TABLES;

【问题讨论】:

  • 想要的结果是什么?
  • 什么是 p2.pricetime,其中每个 p1.pricetime 的 p2.price = max(p2.price)?

标签: mysql sql correlated-subquery


【解决方案1】:

我猜这是你想得到下一个价格的查询:

select p.*,
       (select p2.price
        from prices p2
        where p2.id > p.id
        order by p2.id
        limit 1
       ) as nextprice
from prices p;

要获得最大的变化,您可以这样做:

select p.*,
       (select p2.price
        from prices p2
        where p2.id > p.id
        order by p2.id
        limit 1
       ) as nextprice
from prices p
order by nextprice - price desc
limit 1;

为了提高性能,您需要在prices(id, price) 上建立索引。

最有效的方法是假设ids 是连续的并且没有间隙。在这种情况下,最好使用自联接:

select p.*, pnext.price
from prices p join
     prices pnext
     on p.id = pnext.id - 1
order by pnext.price - p.price desc
limit 1;

【讨论】:

    【解决方案2】:

    谢谢 Gordon,当我问这个问题时,stackoverflow 建议将相关子查询作为标签。答案就在其中。所以这里是:

    最大增加时间:

    SELECT p1.pricetime starttime, min(p4.pricetime) endtime, 
    p1.price startingprice, p4.price maxnextprice
    FROM prices p1 inner join prices p4 on p4.id > p1.id
    WHERE p4.price = 
    (SELECT max(p3.price) 
    FROM prices p2 inner join prices p3 on p3.id > p2.id 
    where p1.id = p2.id 
    group by p2.pricetime order by max(p3.price) limit 1)
    group by p1.pricetime, p4.price;
    

    感谢您的意见。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 2022-01-05
      • 2016-12-28
      • 1970-01-01
      相关资源
      最近更新 更多