我正在尝试从许多不同的表中进行选择,对于其中一个限制,主键不能有一个日期在过去30天内的项与其关联。但是,主键可以有多个与之关联的项。
我目前面临的问题是,当有多个项目关联时,一个在日期范围内,另一个在日期范围外,它仍然会被返回。如果任何相关项目的日期在过去30天内,我希望将其排除在外。
如何让程序获取所有的项目,而不是一次只获取一个项目?
谢谢!
发布于 2018-07-14 04:43:23
使用NOT IN
select *
from table
where id not in (select id from table where datefield > dateadd(day,-30,getdate())这只返回table中的记录,其中id没有datefield超过30天的记录。您可能还需要一个连接,因为您引用了许多不同的表。就像..。
select *
from table
where id not in (select id
from table
inner join table2 on table2.refid = table.id
where table2.datefield > dateadd(day,-30,getdate())https://stackoverflow.com/questions/51332478
复制相似问题