简体   繁体   中英

Left Join With a Where Clause on Left Table

I want to filter my SQL results between two given dates while also joining a table on a condition. This is the SQL I'm currently using:

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;

If I move the event.dt BETWEEN to the join (ie, on entity.id = . . . and event.dt BETWEEN ), it doesn't appear to filter the results at all, returning me a ton of results outside of the date range specified.

Try using the join like so

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;

Your query should be like this

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM