我的select语句中包含Case in Where子句的语法错误

时间:2015-10-17 22:28:00

标签: mysql

我是MySQL新手。我有一个select语句,我一直在努力,我得到一个语法错误(错误代码:1064)。在下面的查询中//////////之间的部分表示语法错误:

SELECT * FROM `customers` WHERE 
First_name !='0' And
Last_name !='0' And

(Phone !='0' And Phone >= 10 And Phone Not like '111%' And Phone Not like '222%' And Phone Not like '333%' And Phone Not like '444%' And Phone Not like '555%' And Phone Not like '666%' And Phone Not like '777%' And Phone Not like '888%' And Phone Not like '999%' And Phone Not like '800%' And Phone Not like '877%' And Phone Not like '866%' And Phone Not like '911%' And Phone Not like '123%' And Phone Not like '1%' And Phone Not like '0%') And

Age !='0' And
Gender !='0' And

////////////////////////////////////////////

(Marital_status !='0' or Case when Marital_status = 'Single' then ((Age between 25 to 75)(Gender='Female' and Annual_income is between $40k to $200k)(Gender='Female')(Zip_code is between 90000 to 93099))or Case when Marital_status='Cohab' then ((Age between 25 to 75)(Annual_income >= 40000)(Zip_code is between 90000 to 93099)) or Case when Marital_status='Married' then ((Age between 25 to 75)(Annual_income is between $40k to $200k)(Zip_code is between 90000 to 93099))) And

/////////////////////////////////////////////

Annual_income !='0' And
Zip_code !='0'

1 个答案:

答案 0 :(得分:0)

我不确定您的架构是什么或您的表结构是什么。我不确定这是否是您在Select中尝试完成的内容,但是您的CASE声明需要看起来更像这样才能在语法上正确:

or Case single when Marital_status = 'Single' then ((Age between 25 and 75) and Gender='Female' and (Annual_income between 40 and 200) and (Zip_code between 90000 and 93099)) Else 1=1 END
or Case cohab when Marital_status='Cohab' then ((Age between 25 and 75) and Annual_income >= 40000 and (Zip_code between 90000 and 93099)) ELSE 1=1 END
or Case married when Marital_status='Married' then ((Age between 25 and 75) and (Annual_income between 40 and 200) and (Zip_code between 90000 and 93099)) Else 1=1 END;

Case Statement syntax

Case statement in where clause

Between in Mysql