问题描述
|
我希望退还自给定日期以来未记录任何订单的所有会员。通过此查询应该很简单。
select * from affiliate where
idUser not in (
select idAffiliate from Orders
where orderDate > \'06/01/11\'
)
会员表的idUser字段是Orders表的idAffiliate的外键。即使我知道我有许多会员公司自本月初以来都没有下订单,但以上记录均未返回任何记录。如果我将日期更改为\ '07 / 01/11 \'-所有联属会员记录都会返回(显然),但如果没有其他要求,则会验证我是否使用正确的实体名称。
非常感激
解决方法
看起来您必须在嵌套查询中将idAffiliate更改为idUser。
在这种情况下,最好使用EXISTS或NOT EXISTS代替IN
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