[英]Sql how to select from a table that does not exist in another table
For example i have 2 tables that have relationship with one another. 例如,我有2个相互关联的表。 room_directory.id and booked_room.room_id room_directory.id和booked_room.room_id
id
room_number
id
booking_id
room_id
How to write query that select only the rooms from room_directory that are not exists in booked_room table? 如何编写查询,仅从room_directory中选择booked_room表中不存在的房间 ?
select * from room_directory where id not in (select room_id from booked_room)
select * from room_directory r where r.id not in (select room_id from booked_room b where r.id = b.room_id);
要么
SELECT * FROM room_directory r WHERE NOT EXISTS (SELECT * from booked_room b WHERE r.id = b.room_id);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.