[英]How to select times from first table which are not available in the second table?
我遇到了這個問題,如果第二個表有多行,則下面的查詢將返回所有字段,而不管其中的內容。
我已經嘗試在開始和結束時間之間使用 not ,當第二個表中只有一行時,它絕對可以正常工作,但是一旦我添加另一行,它就會返回所有內容。
declare @firstTable table (time time(0))
insert @firstTable(time) values ('08:30'),('09:45'),('11:00'),('12:15'),('13:30'),('14:45'),('16:00'),('17:15'),('18:30')
declare @secondTable table (startTime time(0), endTime time(0))
insert @secondTable(startTime, endTime) values ('08:30','10:45'),('13:30','17:00')
select distinct f.time
from @firstTable f, @secondTable s
where f.time not between s.startTime and s.endTime
使用上面的代碼,預期的解決方案應該是 11:00、12:15、17:15 和 18:30,但它總是返回。
我認為not exists
做你想要的:
select *
from @firstTable ft
where not exists (select 1
from @secondTable st
where ft.time >= st.startTime and ft.time <= st.endTime
);
這是db<>小提琴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.