Answer the question
In order to leave comments, you need to log in
Why does a query not work correctly when using UNION in a stored procedure in MySQL?
If everything works correctly in a stored procedure with such a query (one number and NULL are returned):
begin
select min(time) t from params where counter_id = 11858 and time >= 3629232000000 union
select max(time) t from params where counter_id = 11858 and time < 3629232000000;
end
begin
select * from (
select max(time) t from params where counter_id = 11858 and time < 3629232000000 union
select min(time) t from params where counter_id = 11858 and time >= 3629232000000) a;
end
Answer the question
In order to leave comments, you need to log in
UPD:
--Был мусор...
IF EXISTS(select MAX(time) FROM params WHERE...)
BEGIN
--Выборка
END
ELSE
BEGIN
SELECT MIN(time) FROM params WHERE...
END
Here is an example of Explain for a query without a problem:
Here is an example of Explain for a query with a problem:
Here is a strange query that returns the correct (desired) result:
Here is a query that works correctly, returning 1 and NULL:
By experience, we managed to find out the following: the problem with union is repeated only then when the columns counter_id and time are indexed with order counter_id > time and counter_id < time.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question