0

i have this MySQL query:

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

This query works fine except when add the following filter:

AND PRECIO.forex_pair_price_rsi >= '80.00' OR 
PRECIO.forex_pair_price_rsi <= '20.00'

The problem is that the last filter do ignores all other filters and the result is get all data that does comply with the last filter.

1
  • 1
    It looks like you don't yet know how to accept an answer. If one or more answers solved your issue, please award the green tick to the answer that you found most helpful. meta.stackexchange.com/q/5234/352329 Feb 11, 2019 at 8:46

4 Answers 4

5

AND is evaluated before OR because AND has a higher precedence. Use parentheses to override the operator precendence:

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'
    )

Or you could rewrite your condition so it's just one expression:

    PRECIO.forex_pair_price_rsi not between '20.01' and '79.99'
0
2

Operator precedence When you use more than one logical operator in an expression, MySQL evaluates the OR operators after the AND operators. This is called operator precedence.1

SELECT true OR false AND false;

Result

true OR false AND false
-----------------------
1


SELECT (true OR false) AND false;

Result

(true OR false) AND false
-------------------------
0

thus you need parantheses for such case

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' 
)
1

I think I would try to tidy up the query by consolidating the ranged logic in the WHERE clause.

SELECT *
FROM forex_pair_signals AS senales
INNER JOIN forex_pair_price AS precio ON senales.forex_pair_price_id = precio.forex_pair_price_id 
INNER JOIN forex_pair AS pares ON precio.forex_pair_id = pares.forex_pair_id
WHERE pares.forex_pair_id = 13
  AND senales.forex_pair_signals_result = 1
  AND precio.forex_pair_price_time BETWEEN '2019-02-01 00:00' AND '2019-02-07 01:00' 
  AND precio.forex_pair_price_rsi NOT BETWEEN '20.00' AND '80.00'
  AND TIME(precio.forex_pair_price_time) BETWEEN '06:00' AND '13:00'
ORDER BY senales.forex_pair_signals_id DESC

Your final WHERE clause condition can be rewritten as NOT BETWEEN which totally avoids the issue.

I also took the liberty to make your table aliases lowercase and I rearranged the WHERE clause conditions to position the simpler/lighter conditions first.

I think this query is much tidier now.

(p.s. Try to avoid using * in your SELECT claues; you should only ask for data that you intend to use.)

1

You should use parenthesis while using OR function, as it corresponds to either one or two WHERE Clause defined within it by differentiating in brackets.

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;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.