Can someone explain to me what syntax this SQL query matches?
SELECT DISTINCT boats.boatid
FROM boats LEFT JOIN reservations ON
reservations.boatid = boats.boatid
and (@paramFromDate <= reservations.to AND reservations.from <= @paramToDate )
WHERE reservations.to IS NULL
tables:
**boats** boatid
**reservations** reservationid, fk_boatid, from, to
The idea of the query is to get non-reserved boats for a date range marked with params. Any boat that has any part of the range covered even partially is unavailable.
How is this “and…” code there? Why is it missing something like WHERE keyword? Seems like WHERE is implicit there?
When you do the JOIN you can have multiple criterion. The AND is what makes that so such that you only join when “this” AND “that” are true. You could also move that to the WHERE clause but SQL can optimize it better if it’s part of the JOIN condition. Not to mention in this case, it may give different results due to the LEFT JOIN. I can’t tell because I’m not sure which tables the max and minprice come from.
In short, it’s part of the join condition.
(Rewrote the code so it’s easier to read sensibly)