只有当值不存在时才返回行我有两张桌子-reservation: id | some_other_column ----+------------------
1 | value 2 | value 3 | value第二张桌子-reservation_log: id | reservation_id | change_type ----+----------------+-------------
1 | 1 | create
2 | 2 | create
3 | 3 | create
4 | 1 | cancel 5 | 2 | cancel我只需要选择没有取消的预订(在本例中仅为ID 3)。我可以简单地选择取消WHERE change_type = cancel条件,但我挣扎着没有取消,因为简单WHERE在这里不管用。
2 回答
温温酱
TA贡献1752条经验 获得超4个赞
SELECT *FROM reservationWHERE id NOT IN (select reservation_id FROM reservation_log WHERE change_type = 'cancel')
SELECT r.*FROM reservation rLEFT JOIN reservation_log l ON r.id = l.reservation_id AND l.change_type = 'cancel'WHERE l.id IS NULL
LEFT JOIN
ON
INNER JOIN
NULL
WHERE l.id IS NULL
添加回答
举报
0/150
提交
取消