SQL 服务器,Select 来自现有日期变量的 dateadd

SQL Server, Select from dateadd of existing date variable

我正在加入 2 个表,它们都有一些日期列。 table2 有列 First_Date 和 Last_Date,table1 的每个索引列都有许多不同的日期。我想加入他们,其中 table1 日期大约是 First_Date 前后 10 天或 Last_Date 前后 10 天。 我有这样的东西:

select * from table1 inner join table2 on table1.index=table2.index
where table1.dates between dateadd(day, 10, table2.First_Date) and
dateadd(day, -10, table2.First_Date) or table1.dates between 
dateadd(day,  10, table2.Last_Date) and
dateadd(day, -10, table2.Last_Date)

我没有从该代码输出,我猜 SQL 被 dateadd 弄糊涂了,因为对于每个索引,First_Date 和 Last_Date 是不同的。

SQL可以和ANDOR混淆,试试加括号。

select * from table1 inner join table2 on table1.index=table2.index
where (table1.dates between dateadd(day, 10, table2.First_Date) and
dateadd(day, -10, table2.First_Date)) or (table1.dates between 
dateadd(day,  10, table2.Last_Date) and
dateadd(day, -10, table2.Last_Date))