左边加入左表的Where子句

时间:2014-01-31 04:23:51

标签: mysql

我希望在两个给定日期之间过滤我的SQL结果,同时在条件上加入表格。这是我正在使用的SQL:

select
   event.location,
   event.dt,
   event.start
from event
where
   event.dt BETWEEN "2014-02-01" AND "2014-02-04"
   left join entity on entity.id = event.entity
   order by dt desc;

如果我将event.dt BETWEEN移动到联接(即entity.id = . . . and event.dt BETWEEN上),它似乎根本没有过滤结果,在指定的日期范围之外返回了大量结果。

2 个答案:

答案 0 :(得分:3)

尝试使用像这样的连接

select  event.location, 
        event.dt, 
        event.start 
from    event left join 
        entity on entity.id = event.entity
where   event.dt BETWEEN "2014-02-01" AND "2014-02-04" 
order by dt desc;

答案 1 :(得分:1)

您的查询应该是这样的

select
   event.location,
   event.dt,
   event.start
from event
   left join entity on entity.id = event.entity
where
   event.dt BETWEEN "2014-02-01" AND "2014-02-04"
   order by event.dt desc;