MySQL separate limit for "where event_date < '2010-01-01' or event_date > '2011-01-01'"
Let's assume we have MySQL table events
from which we'll select a data. Now we have the following SQL:
select *
from events
where event_date < '2010-01-01' or event_date > '2011-01-01'
So, we can get an items collection that's before 2010-01-01 and after 2011-01-01. I suppose that my question is extremely开发者_JAVA技巧 silly but can one limit "left" (that are before 2010-01-01) and "right" (that are after 2011-01-01) records to some number separately in one query (without executing two 'select' queries)?
It depends on what you mean by "query." If you look at the MySQL syntax for the select statement on http://dev.mysql.com/doc/refman/5.0/en/select.html you see that the LIMIT clause goes outside the where clause, but the UNION (see http://dev.mysql.com/doc/refman/5.0/en/union.html) can go outside the LIMIT.
But a UNION is still one query in the sense of one trip to the server.
So the answer is yes and no. The word SELECT appears twice, but there is only one official query. That means the database query planner will do its best, so no worries on your part.
Example:
(select * from events where event_date < '2010-01-01' limit 5)
union
(select * from events where event_date > '2011-01-01' limit 20)
精彩评论