我希望返回任何从属谁没有记录任何订单自给定的日期。通过这个查询应该是非常简单的。
select * from affiliate where
idUser not in (
select idAffiliate from Orders
where orderDate > '06/01/11'
)
affiliate表有一个idUser字段,它是Orders表的idAffiliate的外键。上面没有返回任何记录,即使我知道我有几十个附属公司谁没有下订单,因为这个月初。如果我将日期更改为'07/01/11‘-所有关联记录都会返回(显然),但如果没有其他信息,则会验证我是否使用了正确的实体名称。
非常感谢
发布于 2011-06-11 21:16:06
看起来您必须在嵌套查询中将idAffiliate更改为idUser。在这种情况下,更好的用法存在或不存在而不是IN
select * from affiliate a
where not exists (
select 1 from Orders where orderDate > '06/01/11'
and Orders.idUser = a.idUser
)
发布于 2011-06-13 21:48:08
使用左连接:
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
https://stackoverflow.com/questions/6316102
复制相似问题