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.
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'
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'
)
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.)
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;
Related
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.
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'
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'
)
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.)
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;
I'm attempting to select data in a date range, but this is not working:
SELECT form_user.email
FROM form_instance JOIN
form_user
ON form_instance.form_user_id = form_user.form_user_id
WHERE form_instance.appointment_date >= '2017-04-13' AND <= '2017-05-11' AND
form_instance.status = 3
this is the issue
form_instance.appointment_date >= '2017-04-13' AND <= '2017-05-11'
this should be
form_instance.appointment_date >= '2017-04-13' AND form_instance.appointment_date <= '2017-05-11'
or use BETWEEN
I have a query that uses multiple UNION ALL. I'm getting a syntax error and I can't find it. I think it has to do with using multiple unions. I'm only using MySQL. Any help is appreciated.
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALL(SELECT a.' at line 41
SELECT Sum(counter) AS counter,
DATE_FORMAT(dtime,'%Y-%m-%d 00:00:00') AS dtime
FROM (
SELECT counter,
dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='980371'
AND counter != 0
UNION ALL
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='979661')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='979661' ))
ORDER BY dtime
AND counter != 0
UNION ALL (this is line 41)
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='984121')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible where dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='984121' ))
ORDER BY dtime
AND counter != 0) AS MainTable
GROUP BY WEEKOFYEAR(dtime)
HAVING SUM(counter) > 0
If I change the 'id=' to id in and include the other id number there and drop the second union all it works. I'm pretty sure it has something to do with the second union all. This statement is built dynamically so I cannot easily change to using in instead of multiple union alls.
Let me know if I'm not explaining this clearly and I'll provide more detail.
check your statement just above the line where you've written (this IS line 41)
Try to remove/update this statement and execute your query.
ORDER BY
dtime
AND counter != 0
we can't use order in between the UNION & UNION ALL. Order by should be at the very last, after all UNION / UNION ALL statements
Im having trouble with this query.
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission
WHERE adm.adm_ReferralDate >= '01/01/2014 00:00:00' AND adm.adm_ReferralDate <= '31/12/2014 00:00:00'
AND adm.adm_PriorSurgery = 'Yes'
AND adm.adm_Consultant <> ''
GROUP BY adm_Consultant
ERROR: General error
this works though, but returns the null values as-well
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission
GROUP BY adm_Consultant
I tried the HAVING clause instead of the WHERE clause, but still it fails.
Please help.
here was my reading material.
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement. The result is a BIGINT value.
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count
You are forgetting to create alias adm
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission adm
WHERE adm.adm_ReferralDate >= '01/01/2014 00:00:00' AND
adm.adm_ReferralDate <= '31/12/2014 00:00:00'
AND adm.adm_PriorSurgery = 'Yes'
AND adm.adm_Consultant <> ''
GROUP BY adm_Consultant
Try using ISO standard date formats:
SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission adm
WHERE adm.adm_ReferralDate >= '2014-01-01' AND adm.adm_ReferralDate <= '2014-12-31' AND
adm.adm_PriorSurgery = 'Yes' AND
adm.adm_Consultant <> ''
GROUP BY adm_Consultant;
I try to run this query:
select * from WorkTbl
where ((Tdate >= '20100414' AND Ttime >= '06:00') and (Tdate <= '20100415' AND Ttime <= '06:00'))
I have this date: 14/04/2010 and time: 14:00
I cant see hem, how to fix the query?
Thank's in advance
Have you tried formatting the date in the where clause like this:
'DD/MM/YYYY'
Your current query will only match rows where Ttime is exactly 06:00 (ie, this is the only time that matches both the >= '06:00' and the <= '06:00 criteria).
Try something like this instead:
SELECT *
FROM WorkTbl
WHERE (Tdate = '20100414' AND Ttime >= '06:00')
OR (Tdate = '20100415' AND Ttime <= '06:00')
SELECT *
FROM WorkTbl
WHERE (Tdate > '20100414' OR (Tdate == '20100414' AND Ttime >= '06:00'))
AND (Tdate < '20100415' OR (Tdate == '20100415' AND Ttime <= '06:00'))