下面的代码工作正常,但现在我要添加多个这样的日期:
预订开始日期=2018-07-16、2018-07-17

SELECT
    *,
    SUM(IF(reservationId IS NULL,
        0,
        reservationStartDate = '2018-07-17')) AS ConflictingReservations
FROM
    units
        LEFT JOIN
    reservations ON (unitsId = reservationSpace)
WHERE
    unitsType = 'room1'
GROUP BY unitsId
HAVING ConflictingReservations = 0;

最佳答案

我只想:

SELECT u.unit_id,
       SUM(r.reservationStartDate IN ('2018-07-17')) AS ConflictingReservations
FROM units u LEFT JOIN
     reservations r
     ON u.unitsId = r.reservationSpace
WHERE u.unitsType = 'room1'
GROUP BY u.unitsId
HAVING ConflictingReservations = 0;

或者,更简单地说:
SELECT u.*
FROM units u LEFT JOIN
     reservations r
     ON u.unitsId = r.reservationSpace AND
        r.reservationStartDate in ( . . . )
WHERE u.unitsType = 'room1' AND r.reservationSpace IS NULL;

完全不需要GROUP BY,因为您没有使用计数。

10-08 19:27