I have the following SQL Query.
SELECT MIN(PLD.available_date) as 'FROM', MAX(PLD.available_date) as 'UNTIL', (
CASE
WHEN DATEDIFF('2012-04-01', '2012-04-10') = (COUNT(PLD.available_date) - 1)
THEN 'Yes'
ELSE 'No'
END) as isAvailable, PL.*
FROM `parking_lot_dates` as PLD
INNER JOIN parking_lots as PL ON PLD.plid = PL.plid
WHERE PLD.available_date BETWEEN '2012-04-01' AND '2012-04-10'
GROUP BY PLD.plid
But is it possible to put that INNER JOIN into a CASE? what i’m trying to accomplish is that when the isAvailable column’s value is Yes then grab the extra information otherwise don’t grab it.
I tried putting the INNER JOIN between a CASE statement but it didn’t work.
Thanks in advance.
You can’t do a conditional join in the way you want, but you could instead:
The subquery performs the grouping operation on the
parking_lot_datestable, and we make an outer join between that and theparking_lotstable in order to have records even when the join condition is not satisfied. Learn about SQL joins.The
WHEREclause imitates the effect of yourINNER JOINby eliminating any results for which there was not a matching record inparking_lotswhen one was expected; it can be omitted if you didn’t actually want that behaviour.