Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL比较没有Id的行_Mysql_Sql - Fatal编程技术网

MySQL比较没有Id的行

MySQL比较没有Id的行,mysql,sql,Mysql,Sql,我有个问题。我试图查询所有MACD为正的记录,从该记录中,我的MySQL服务器上的前一条记录有一个负的MACD。以下是我的表格示例: +---------------------+------+ | DateTimeGMT0 | MACD | +---------------------+------+ | 2021-04-26 01:30:00 | 12 | | 2021-04-25 03:45:00 | 1 | | 2021-04-22 10:56:00 | -5

我有个问题。我试图查询所有MACD为正的记录,从该记录中,我的MySQL服务器上的前一条记录有一个负的MACD。以下是我的表格示例:

+---------------------+------+
|    DateTimeGMT0     | MACD |
+---------------------+------+
| 2021-04-26 01:30:00 | 12   |
| 2021-04-25 03:45:00 | 1    |
| 2021-04-22 10:56:00 | -5   |
| 2021-04-22 02:01:00 | -20  |
+---------------------+------+
现在有一条重要规则:

不能在查询中使用Id列,因为它们不在 正确顺序

我已经找到了这个帖子:

但不幸的是,他使用Id列。我实际上不知道如何查询前一行

就我而言,想要的结果是:

+---------------------+------+
| 2021-04-25 03:45:00 | 1    |
+---------------------+------+
但是当有更多的时候,我想要所有的,所以不仅仅是一个

有人能帮我吗?

用lag


谢谢很有魅力
select t.*
from (select t.*,
             lag(macd) over (order by DateTimeGMT0) as prev_macd
      from t
     ) t
where mac > 0 and prev_macd < 0;