我不确定这是否可行,但我知道如果是,stackoverflow社区就是我能找到答案的地方。我基本上试图预处理一些数据,以便我可以加速我的应用程序。这是我正在尝试做的一个例子:
SELECT id
FROM data AS data1
WHERE rangeVal < (SELECT rangeVal FROM data WHERE id = data1.id - 1)
AND actiondate >= '1980-05-18'
AND actiondate <= '2012-05-18'
ORDER BY actiondate DESC
基本上,我只是在他们的rangeVal小于第二天的rangeVal时才尝试获取我的数据的ID。
非常感谢任何正确方向的帮助或分数!!
答案 0 :(得分:1)
如果您的日子是增量的,并且每天都有一行,而不是尝试依赖从增量ID中减去,这可能由于行删除和其他问题而非常不可靠,您可以使用DATE_SUB()
或DATE_ADD()
作为加入条件:
SELECT
id
FROM
data data_today
/* JOIN on a date manipulation */
JOIN data data_tomorrow ON DATE_ADD(data_today.actiondate, INTERVAL 1 DAY) = data_tomorrow.action_date
WHERE
data_today.rangeVal < data_tomorrow.rangeval
AND data_today.actiondate BETWEEN '1980-05-18' AND '2012-05-18'
答案 1 :(得分:0)
子查询也很好
SELECT id
FROM data AS data1
WHERE rangeVal <
(
SELECT rangeVal FROM data d2 WHERE d2.id = data1.id AND d2.actiondate=
DATE_ADD(d2.action_date, INTERVAL 1 DAY)
)
AND actiondate BETWEEN '1980-05-18' AND '2012-05-18'
ORDER BY actiondate DESC