Sql如何从另一个表中不存在的表中进行选择

时间:2014-06-25 15:21:14

标签: php mysql

例如,我有2个彼此有关系的表。 room_directory.id booked_room.room_id

room_directory

id
room_number

booked_room

id
booking_id
room_id

如何编写只选择 room_directory registered_room 表中不存在的会议室的查询?

2 个答案:

答案 0 :(得分:0)

select * from room_directory where id not in (select room_id from  booked_room)

答案 1 :(得分:0)

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);