简体   繁体   English

SQL如何从另一个表中不存在的表中选择

[英]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.idbooked_room.room_id

room_directory room_directory

id
room_number

booked_room booked_room

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.

相关问题 PHP-SQL如何从表中选择ID并插入到另一个表中? - PHP - SQL how to select ids from table and insert into another table? SELECT表并从另一个表获取一些数据(如果存在) - SELECT table and get some data from another table if they exist 从表中选择其他表中不存在的行 - Select row from a table that does not exist in other table 如何从另一个表中没有特定值的UID的表中选择*? - How can I select * from a table where a UID doesn't exist with a certain value in another table? PHP / SQL:如何从一个表中选择完全不同的表中不存在的值 - PHP/SQL: How to SELECT values from one table that do not exist in a completely different table 如何在另一张桌子上不存在 - How to not exist on another table SQL从一个表中选择不在另一个表中的项 - SQL select item from one table that is not in another 从表中选择另一个sql的结果 - select from table with result of another sql SQLSTATE[42P01]:未定义表:7 错误:关系“类别”不存在第 1 行:从“类别”中选择 * ^(SQL:从“类别”中选择 *) - SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories") 从一个表上存在但另一表上不存在的数据中选择 - select from data which exist on one table but do not exist on another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM