比较运算符在MySQL查询中不起作用

Alf*_*ide 1 mysql sql join

我有这个MySQL查询:

SELECT * FROM forex_pair_signals AS SENALES 
JOIN forex_pair_price AS PRECIO 
ON SENALES.forex_pair_price_id = PRECIO.forex_pair_price_id 
JOIN forex_pair AS PARES 
ON PRECIO.forex_pair_id = PARES.forex_pair_id
WHERE 
PRECIO.forex_pair_price_time >= '2019-02-01 00:00' AND
PRECIO.forex_pair_price_time <= '2019-02-07 01:00' AND 
TIME (PRECIO.forex_pair_price_time) BETWEEN '06:00' AND '13:00' AND 
PARES.forex_pair_id = 13 AND SENALES.forex_pair_signals_result = 1 AND
PRECIO.forex_pair_price_rsi >= '80.00' 
OR PRECIO.forex_pair_price_rsi <= '20.00' 
ORDER BY SENALES.forex_pair_signals_id DESC
Run Code Online (Sandbox Code Playgroud)

此查询工作正常,除非添加以下过滤器:

AND PRECIO.forex_pair_price_rsi >= '80.00' OR 
PRECIO.forex_pair_price_rsi <= '20.00'
Run Code Online (Sandbox Code Playgroud)

问题是最后一个过滤器会忽略所有其他过滤器,结果是获得符合最后一个过滤器的所有数据.

And*_*mar 5

AND之前进行评估,OR因为AND它具有更高的优先级.使用括号覆盖运算符优先级:

WHERE
    PRECIO.forex_pair_price_time >= '2019-02-01 00:00' AND
    PRECIO.forex_pair_price_time <= '2019-02-07 01:00' AND 
    TIME (PRECIO.forex_pair_price_time) BETWEEN '06:00' AND '13:00' AND 
    PARES.forex_pair_id = 13 AND SENALES.forex_pair_signals_result = 1 AND
    (
        PRECIO.forex_pair_price_rsi >= '80.00' 
        OR
        PRECIO.forex_pair_price_rsi <= '20.00'
    )
Run Code Online (Sandbox Code Playgroud)

或者您可以重写您的条件,因此它只是一个表达式:

    PRECIO.forex_pair_price_rsi not between '20.01' and '79.99'
Run Code Online (Sandbox Code Playgroud)