SQL Left Join 2表where Condition

时间:2014-06-24 21:32:36

标签: mysql sql join left-join

我有2张桌子:AllClients& AllPolicies。我试图找到没有"活跃政策的客户记录"。活动策略是一种策略,其生效日期在今天之前,到期日期在今天之后。这是我提出的sql但它不会执行:

SELECT ac.LookupCode
  FROM AllClients ac
  LEFT JOIN AllPolicies ap 
    ON ap.LookupCode = ac.LookupCode
   AND ap.EffectiveDate  < '2014-06-24'
   AND ap.ExpirationDate > '2014-06-24'

我正在使用mySQL,并且我得到了一些奇怪的&#34;未知的表状态:TABLE_TYPE&#34;错误。我非常自信我的SQL语句是垃圾;)感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

SELECT LookupCode
FROM AllClients
where LookupCode not in 
    (select LookupCode 
    from AllPolicies
    where EffectiveDate < '2014-06-24' and ExpirationDate > '2014-06-24')

答案 1 :(得分:-1)

这可能会有所帮助。您还没有充分阐述mySQL错误,它们可能与您的表结构或服务器设置有关。

SELECT
 ac.LookupCode
FROM
 AllClients ac
 LEFT JOIN AllPolicies ap ON ap.LookupCode = ac.LookupCode
  AND ap.EffectiveDate < @Today
  AND ap.ExpirationDate > @Today
WHERE
 ap.LookupCode IS NULL --This means do not get records that have active policies

答案 2 :(得分:-1)

> SELECT AllClients.LookupCode FROM AllClients Left Join AllPolicies on
> AllPolicies.LookupCode = AllClients.LookupCode
>      and date(AllPolicies.EffectiveDate) < date '2014-06-24'
>      and date(AllPolicies.ExpirationDate) > date '2014-06-24';

答案 3 :(得分:-1)

故障排除如下。从这开始:

select ac.*
from AllClients ac
where 1 = 2

如果成功运行,请转到此

select ac.LookUpCode
from AllClients ac
where 1 = 2

然后这个:

select ac.LookUpCode
from AllClients ac left AllPolicies ap on 1 = 2
where 1 = 2

继续前进,一次添加一位。一旦出现错误,您就会知道是什么原因造成的。