Tsql 大脑冻结

Tsql 大脑冻结,tsql,Tsql,我期待着返回任何会员谁没有记录任何订单以来,给定的日期。通过这个查询应该非常简单 select * from affiliate where idUser not in ( select idAffiliate from Orders where orderDate > '06/01/11' ) 附属表有一个idUser字段,该字段是Orders表的idAffiliate的外键。尽管我知道自本月初以来,我有几十家附属公司没有下订单,但上面没有返回任何记录。如果我将日期更改为

我期待着返回任何会员谁没有记录任何订单以来,给定的日期。通过这个查询应该非常简单

select * from affiliate where 
idUser not  in ( 
  select idAffiliate from Orders
  where orderDate > '06/01/11'
)
附属表有一个idUser字段,该字段是Orders表的idAffiliate的外键。尽管我知道自本月初以来,我有几十家附属公司没有下订单,但上面没有返回任何记录。如果我将日期更改为“07/01/11”-所有分支机构记录都会返回(显然),但如果没有其他记录,则会验证我使用的是正确的实体名称


非常感谢

看起来您必须在嵌套查询中将idAffiliate更改为idUser。 在这种情况下,更好的使用存在或不存在,而不是存在

select * from affiliate a 
where not exists ( 
  select 1 from Orders where orderDate > '06/01/11'
  and Orders.idUser = a.idUser
)

看起来您必须在嵌套查询中将idAffiliate更改为idUser。 在这种情况下,更好的使用存在或不存在,而不是存在

select * from affiliate a 
where not exists ( 
  select 1 from Orders where orderDate > '06/01/11'
  and Orders.idUser = a.idUser
)
使用左联接:

select a.* 
from affiliate a
left join Orders o
    on a.idUser= o.idAffiliate
    and o.orderDate > '06/01/11'
where o.idAffiliate is null
使用左联接:

select a.* 
from affiliate a
left join Orders o
    on a.idUser= o.idAffiliate
    and o.orderDate > '06/01/11'
where o.idAffiliate is null

orderdate是否为字符串类型?如果是这样,我认为你应该使用ansi日期格式。YYYYMMDD。您的日期是否以YY/MM/DD(或任何奇怪的格式)进行分析日期类型为“smalldatetime”。e、 g:2008-01-28 15:06:00订单日期是字符串类型吗?如果是这样,我认为你应该使用ansi日期格式。YYYYMMDD。您的日期是否以YY/MM/DD(或任何奇怪的格式)进行分析日期类型为“smalldatetime”。e、 g:2008-01-28 15:06:00Thankx-只是为了存档。。。它应该是'and Orders.idAffiliate=a.iduser'Thankx-只是为了存档。。。它应该是'and Orders.idAffiliate=a.iduser'