i was trying to run this query:
(
(SELECT * FROM (SELECT * FROM `users` WHERE `access_level` > 0) AS `search_subject` WHERE (BINARY `username` = ?))
UNION
(SELECT * FROM (SELECT * FROM `users` WHERE `access_level` > 0) AS `search_subject` WHERE (BINARY `username` = ?))
)
LIMIT 5
but got an error because of the surrounding parenthesis, just before LIMIT 5. obs: the query is all in a row, i endented it here for better reading. is it allowed in sql?
the following works fine:
(SELECT * FROM (SELECT * FROM `users` WHERE `access_level` > 0) AS `search_subject` WHERE (BINARY `username` = ?))
UNION
(SELECT * FROM (SELECT * FROM `users` WHERE `access_level` > 0) AS `search_subject` WHERE (BINARY `username` = ?))
LIMIT 5
my question is: this second version is equivalent to the first one or does the LIMIT in the second version only applies in the second SELECT? if so, what can i do to work around this? thanks (:
ps: don't worry about the interrogation marks. that's just because of the prepared statements
Actually, you don’t need the the brackets:
There’s no difference in either side of the UNION to begin with, so there’s no need for it.
No, the LIMIT applies to the result of all the UNION’d queries, as does the ORDER BY clause.