简体   繁体   English

选择条件+其他条件取决于订购的结果

[英]Select condition + another condition depending on the ordered result

    date      action 
----------   -------
2018-01-12   acquire
2018-01-14   release
2018-01-15   acquire
2018-01-19   release

And I need to SELECT WHERE date>='2018-01-13' but only from some 'acquire' row (ordered by date ASC) so the result would be 我需要选择WHERE date> ='2018-01-13'但仅从某些'acquire'行中(按日期ASC排序),因此结果将是

2018-01-15   acquire
2018-01-19   release

(from the first acquire after the date condition is met). (从满足日期条件后的第一次获取开始)。 Is it possible with a single SQL sentence? 一个SQL语句可能吗?

Get the minimum date that meets the condition, then select all rows with dates starting from that. 获取满足条件的最短日期,然后选择所有从该日期开始的日期。

SELECT date, action
FROM yourTable
WHERE date >= (
    SELECT MIN(date)
    FROM yourTable
    WHERE date >= '2018-01-13' AND action = 'acquire'
)
SELECT a.* 
  FROM my_table a 
  JOIN my_table b 
    ON b.date <= a.date 
 WHERE b.date >= '2018-01-13' 
   AND b.action = 'acquire';

Assuming you are trying to get the first date for each action ... 假设您尝试获取每个动作的第一个日期...

SELECT `date`, `action`
FROM theTable
WHERE (`date`, `action`) IN (
   SELECT MIN(`date`), `action`
   FROM theTable
   WHERE `date` > '2018-01-13'
   GROUP BY `action`
)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM