SQL脑冻结

问题描述

| 我希望退还自给定日期以来未记录任何订单的所有会员。通过此查询应该很简单。
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
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...