I am trying to find the details of villas that have never been booked. However my query is returning no values just the column names.
Table insertion
Insert into villa values('V14', 'Serene Bliss', '785.95', 'T1'),
('V15', 'Serene no', '585', 'T2'),
('V16', 'Serene yesBliss', '395', 'T3'),
('V17', 'Serene wowBliss', '995', 'T4'),
('V18', 'Serene yayBliss', '700', 'T5'),
('V20', 'Lonelyislan', '200', 'T6');
Insert into Reservation values
('R321', '2020-06-12','C345'),
('R322', '2020-06-13','C745'),
('R323', '2020-06-14','C645'),
('R421', '2020-06-15','C545'),
('R521', '2020-06-16','C445');
Insert into VillaReservation values
('2020-12-01','2020-12-10','V14', 'R321'),
('2020-11-01','2020-11-10','V15', 'R322'),
('2020-10-01','2020-10-10','V16', 'R323'),
('2020-09-01','2020-09-10','V17', 'R421'),
('2020-12-08','2020-08-10','V18', 'R521'),
('2020-12-15','2020-11-10','V18', 'R521');
Query
SELECT v.villaid,
villaname,
villacostperday,
villatypeid
FROM villa v,
villareservation vr
WHERE v.villaid = vr.villaid
AND vr.reservationid = NULL;
Use left join
with a where condition vr.reservationid is null
SELECT v.villaid,
villaname,
villacostperday,
villatypeid
FROM villa v
LEFT JOIN villareservation vr ON v.villaid = vr.villaid
WHERE vr.reservationid IS NULL
If you want villas that have never booked, NOT EXISTS
seems the most direct route:
SELECT v.*
FROM villa v
WHERE NOT EXISTS (SELECT 1
FROM villareservation vr
WHERE v.villaid = vr.villaid
)
;
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.