我需要编写一个SQL查询,该查询将删除所有为空的书。
到目前为止,我已经创建了一个查询,该查询显示哪些书为空:
查询,显示哪些书为空
现在,我需要计算出SQL查询以删除所有已清空的书籍。我以为查询将是:
答案 0 :(得分:1)
您可以在下面尝试
DELETE FROM BOOK
WHERE EXISTS (SELECT 1 FROM BOOK B
FULL JOIN CHECKOUT C ON C.BOOK_NUM = B.BOOK_NUM
WHERE C.CHECK_OUT_DATE IS NULL);
答案 1 :(得分:0)
DELETE FROM BOOK
WHERE BOOK_NUM IN
(SELECT B.BOOK_NUM
FROM BOOK B FULL JOIN CHECKOUT C ON C.BOOK_NUM = B.BOOK_NUM
WHERE C.CHECK_OUT_DATE IS NULL);
内部查询将选择所有没有check_out_date的book_num。 如果内部查询返回book_num,则外部查询将删除books表中存在的书籍。
答案 2 :(得分:0)
我不确定FULL JOIN
在定义良好的数据模型中正在做什么。想必您想要:
DELETE FROM BOOK
WHERE NOT EXISTS (SELECT 1
FROM CHECKOUT C
WHERE C.BOOK_NUM = B.BOOK_NUM AND
C.CHECK_OUT_DATE IS NOT NULL
);