关于SQL语法的澄清

关于SQL语法的澄清,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,关于我的SQL语法的澄清 WHERE条件是否仅使用现金或' 和PLANNAME=NULL select * from report rpt left join InsurancePlan Ip on IP.PlanName = rpt.PrimaryPlanName where (rpt.InsuranceCode in ('CASH', '') or Ip.PlanName is null) 按照现在的方式,您将获得insuranceCode=cash和insuranceCode=

关于我的SQL语法的澄清

WHERE
条件是否仅使用
现金或
'

PLANNAME=NULL

select * 
from report rpt
left join InsurancePlan Ip on IP.PlanName  = rpt.PrimaryPlanName
where
   (rpt.InsuranceCode in ('CASH', '') or Ip.PlanName is null)

按照现在的方式,您将获得insuranceCode=cash和insuranceCode='',或者planName为null。因此,无论insCode如何,所有planeName=null都将显示,planeName的代码值为“”或cash。
如果希望所有行的planName为空,请将或更改为和,基本上,您的查询将返回保险代码等于“现金”或“”的行,以及planName为空值的行,
优先,这意味着您将有效地获得具有这两个条件之一或两者的任何行,即

  • 如果
    rpt.InsuranceCode
    的值为
    CASH
    或空字符串
  • 如果
    Ip.PlanName
    为空
  • 如果其中一个或两个条件都满足,您将在结果集中得到一行

    您也可以将查询视为以下内容:

    WHERE
        rpt.InsuranceCode = 'CASH'
     OR rpt.InsuranceCode = ''
     OR Ip.PlanName IS NULL
    
    在下例表中,将在结果集中结束的行在右侧用*标记:

    InsuranceCode    PlanName    ResultSet    Why?
    'CASH'           'Whatever'      *        rpt.InsuranceCode = 'CASH'
    ''               'Whatever'      *        rpt.InsuranceCode = ''
    'Whatever'       NULL            *        Ip.PlanName IS NULL
    'CASH'           NULL            *        Both
    ''               NULL            *        Both
    'Whatever'       'Whatever'               Neither
    

    不确定你在问什么。你能澄清,解释更多吗?这个答案对澄清任何事情都没有多大作用。首先,保险代码不可能既是现金又是空字符串,因此其中的“and”放错了位置。