Sql错误说"语法错误" ="

时间:2017-04-05 10:45:15

标签: php sql sql-server sql-server-2008

在这个sql中,我只想查看" ON" b.block =' False'。因为有时" profileview"表值为空。 我只想要,如果user_fullinfo值在profileview中与此条件匹配,则b.block =' False'它应该是显示,如果在配置文件视图中没有值,那么它也应该显示,

SELECT  a.* FROM user_fullinfo a left join profileview b on
CASE
WHEN a.id=b.rid and b.rid='105' THEN 
b.block='False' 
END
where gender != 'Male' and (DATEDIFF(YY,dob,GETDATE()) Between '10' and '100') and (a.heightid between 58 and 65) and( a.complexionid = '3' or a.complexionid = '4' or a.complexionid = '5') and a.bodytypeid = '4' and a.marital_statusid = '3'

它说sql错误(102):" =" ...附近的语法不正确 请给出解决方案..

2 个答案:

答案 0 :(得分:0)

布尔表达式不适合作为值,例如case的值。我的建议是完全删除case并使用布尔逻辑。像这样:

select a.*
from user_fullinfo a left join
     profileview b
     on (a.id = b.rid and b.rid = '105' and b.block = 'False') or 
        ( not (a.id = b.rid and b.rid = '105' and b.block = 'False') and
          gender <> 'Male' and (datediff(year, dob, getdate()) Between 10 and 100) and
          (a.heightid between 58 and 65) and
          (a.complexionid in (3, 4, 5)) and
          a.bodytypeid = 4 and a.marital_statusid = 3
        );

注意:

  • SQL中SQL不等式的标准是<>,尽管!=也被广泛接受。
  • 不要在数字常量周围使用单引号。 between '10' and '100'between 10 and 100不同。
  • 我怀疑这个版本或你的case版本真的能做到你想要的。对于第二个条件,两个表之间没有相关性。

这应解决您的语法问题。逻辑问题是一个不同的问题。我建议您询问另一个问题,包括样本数据,期望,结果和逻辑解释。

答案 1 :(得分:0)

正如你在case语句中只有1个条件,你可以使用直接连接,如下面的

jobname     | jobid | Action
sendemail   | 34636 | Button(Name Retry)
sendereport | 35455 | Button(Name Retry)

如果您有任何其他需要添加的案例陈述,您可以像这样更改ON语句

SELECT  
    a.* 
    FROM user_fullinfo a 
        LEFT JOIN profileview b 
            ON a.id = b.rid 
                AND b.rid ='105' 
                AND b.block ='False' 
        WHERE gender <> 'Male' 
            AND (DATEDIFF(YY,dob,GETDATE()) BETWEEN '10' AND '100') 
            AND (a.heightid between 58 and 65) 
            AND 
            (
                a.complexionid = '3' 
                OR 
                a.complexionid = '4' 
                OR 
                a.complexionid = '5'
            ) 
            AND a.bodytypeid = '4' 
            AND a.marital_statusid = '3'