如何让MySQL case语句在存储过程中工作?

时间:2012-06-19 11:03:34

标签: mysql select case

我正在尝试使用MySQL CASE语句从产品表中选择产品。

我有这个:

SELECT art.a,art.b,art.c
FROM products AS art
   WHERE art.inventory != "0"
   # filter by param_reference_iln (seller ID = vARCHAR(13) )
   CASE        
    WHEN param_reference_iln = '3333333333333' THEN AND art.iln = '1111111111111' OR art.iln = '2222222222222'
    WHEN param_reference_iln = '8888888888888' THEN AND art.iln = '7777777777777'
    ELSE AND art.iln NOT IN ('1111111111111','2222222222222','7777777777777')
   END CASE;

我已经尝试了CASE variants,但我无法继续工作。

问题
我究竟做错了什么?

3 个答案:

答案 0 :(得分:1)

我不确定WHERE子句中是否有CASE。 无论如何,你应该在这里使用严格的布尔语法:

SELECT art.a,art.b,art.c
FROM products AS art
WHERE art.inventory != "0"
    AND (
        (param_reference_iln = '3333333333333' AND (art.iln = '1111111111111' OR art.iln = '2222222222222'))
        OR
        (param_reference_iln = '8888888888888' AND art.iln = '7777777777777')
        OR
        art.iln NOT IN ('1111111111111','2222222222222','7777777777777')
    );

答案 1 :(得分:1)

您不能使用CASE动态构造where子句,方法是让它返回THEN AND ...等条件语句,并期望AND适用于前一个条件

您需要使用vanilla内联逻辑;

WHERE art.inventory != "0"
AND (
    (param_reference_iln = '3333333333333' AND art.iln IN ('1111111111111', '2222222222222'))
  OR  
    (param_reference_iln = '8888888888888' AND  art.iln = '7777777777777') 
  OR
    (art.iln NOT IN ('1111111111111','2222222222222','7777777777777'))
)

答案 2 :(得分:0)

不是选择,而是存储过程。向下滚动您发布的页面,您将看到