MySQL“where event_date<'2010-01-01'或event_date>'2011-01-01'的单独限制

时间:2011-08-31 04:33:51

标签: mysql mysqli

假设我们有MySQL表events,我们将从中选择一个数据。现在我们有以下SQL:

select *
from events
where event_date < '2010-01-01' or event_date > '2011-01-01'

因此,我们可以获得2010-01-01之前和2011-01-01之后的项目集合。我想我的问题非常愚蠢但是可以在一个查询中分别将“左”(在2010-01-01之前)和“右”(在2011-01-01之后)的记录限制为某个数字(不执行)两个'选择'查询)?

1 个答案:

答案 0 :(得分:1)

这取决于“查询”的含义。如果你查看http://dev.mysql.com/doc/refman/5.0/en/select.html上的select语句的MySQL语法,你会看到LIMIT子句超出了where子句,但是UNION(参见http://dev.mysql.com/doc/refman/5.0/en/union.html)可以超出LIMIT。

但就一次访问服务器而言,UNION仍然是一个查询。

所以答案是肯定的,不是。单词SELECT出现两次,但只有一个官方查询。这意味着数据库查询规划器将尽力而为,所以您不必担心。

示例:

(select * from events where event_date < '2010-01-01' limit 5)
union
(select * from events where event_date > '2011-01-01' limit 20)