简体   繁体   English

mysql相关子查询:选择field1,其中max(field2)

[英]mysql correlated subquery : select field1 where max(field2)

What is the most efficient way to select the time where price has increased the max? 选择价格增加最高价的时间最有效的方法是什么? [Structure at the bottom] [底部结构]

-- get max increased price -获得最高涨价

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

what is p2.pricetime where p2.price = max(p2.price) for each p1.pricetime? 什么是p2.pricetime,其中每个p1.pricetime的p2.price = max(p2.price)?

-- get time of max 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

that is a horribly inefficient way for multi million row tables I'm sure you could do something like this in MSSQL : 对于数百万个行表,这是一种极其低效的方式,我相信您可以在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 ...

which accesses a subquery alias from outside the subquery? 哪个从子查询外部访问子查询别名?

-- structure : - 结构体 :

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;

I am guessing that this is the query that you want to get the next price: 我猜这是您想要获得下一个价格的查询:

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

To get the maximum change, you can do: 要获得最大的变化,您可以执行以下操作:

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;

For performance, you want an index on prices(id, price) . 为了提高性能,您需要一个prices(id, price)的索引。

The most efficient way is to assume that the id s are sequential and have no gaps. 最有效的方法是假设id是顺序的并且没有间隙。 In that case, a self-join is best: 在这种情况下,最好使用自联接:

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;

Thanks Gordon, when I asked the question stackoverflow suggested correlated-subquery as a tag. 谢谢戈登,当我问这个问题时stackoverflow建议将相关子查询作为标签。 Therein lied the answer. 其中撒谎了答案。 So here goes : 因此,这里去:

The time of maximum increase : 最大增加时间:

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;

Thanks for your input. 感谢您的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MYSQL仅在field1或field2不等于文本的情况下选择field1,field2 - MYSQL select field1, field2 only where field1 or field2 does not equal text MySQL-选择field1和field2都大于0的地方 - MySQL - Select where both field1 and field2 are greater than 0 MySQL IF函数-WHERE&#39;field1&gt; 0&#39;吗? &#39;field2 &lt;field1&#39;:&#39;&#39; - MySQL IF function - WHERE 'field1 > 0' ? 'field2 < field1' : '' Mysql在哪里查询…选择* where field1 + field2 + field3 &lt;=字段4 - Mysql where query… select * where field1 +field2 + field3 <= field 4 CodeIgniter其中值IN(Field1,Field2) - CodeIgniter Where value IN (Field1,Field2) (MySQL)OrderBy Field1 = 3,Field2 - (MySQL) OrderBy Field1=3, Field2 从select1字段1插入field1,field2,field2仅填充1个字段 - Inserting into field1,field2 from a select field1, field2 populates only 1 field MYSQL (field1, field2) = (x,y) vs field1= x AND field2 = Y 之间的差异 - MYSQL Difference between ( field1, field2 ) = (x,y) vs field1= x AND field2 = Y Select 其中 field1 或 field2 或 field3 等于电话号码 - Select where field1 or field2 or field3 is equal to a phone number 如何插入到TABLE1(field1,field2&#39;value&#39;,&#39;myval&#39;,&#39;currentdate&#39;)从TABLE2选择(field1,field2) - How to insert into TABLE1 (field1,field2 'value','myval','currentdate') select(field1,field2) from TABLE2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM