how to apply limit with union in Mysql query ?
We have a condition that we want to fetch some data from different tables and combined them with onion in Mysql . Which is very simple we can do this :
and so on …
but in above queries if we want to apply the limit, than it will give error .
here we will discussing what we can do to achieve the results.
If we have multiple query set than how can we use limit and union them all ?
we can do this by separating the query set with () – brackets and merge them with union in mysql , For example we have two tables :-
(SELECT name,city FROM T1 WHERE id < 3 LIMIT 1)
(SELECT name,city FROM T2 WHERE ORDER BY id desc LIMIT 3);
If we do this above like this :-
SELECT name,city FROM T1 WHERE id < 3 LIMIT 1
SELECT name,city FROM T2 WHERE ORDER BY id desc LIMIT 3
Than it will give error ……