MySQL case语句错误,数据未显示部分查询

时间:2015-02-19 15:38:21

标签: mysql

MySQL Case语句错误:

当我使用以下内容时,作为单独的情况,查询有效,但是当它们组合时它们不会。

我正在使用MySQL Workbench

错误:
仅返回制造商信息,而不是基于ProdID的查询 ProdID是正确的,我用它在此之前提取ID并返回正确的ID#。

我在1列中要求它们在下一步中运行计算。

不工作:

CASE 
    When(T4.manufacturers_id = 1) then ''
    When(T4.manufacturers_id = 2) then '.10'
    When(T4.manufacturers_id = 3) then '.10'
    When(T4.manufacturers_id = 4) then '0'
    When(T3.products_id = 11) then '.10'
    When(T3.products_id = 34) then '.10'
    When(T3.products_id = 35) then '.10'
    When(T3.products_id = 36) then '.10'
    When(T3.products_id = 37) then '.10'
    When(T3.products_id = 38) then '.10'
    When(T3.products_id = 39) then '.10'
end As Comms,

工作:

CASE 
    When(T4.manufacturers_id = 1) then ''
    When(T4.manufacturers_id = 2) then '.10'
    When(T4.manufacturers_id = 3) then '.10'
    When(T4.manufacturers_id = 4) then '0'
end As MIDComms,

case
    When(T3.products_id = 11) then '.10'
    When(T3.products_id = 34) then '.10'
    When(T3.products_id = 35) then '.10'
    When(T3.products_id = 36) then '.10'
    When(T3.products_id = 37) then '.10'
    When(T3.products_id = 38) then '.10'
    When(T3.products_id = 39) then '.10'
end As PIDComms,

理想情况下,我希望:

    CASE 
    When(T4.manufacturers_id = 1) then ''
    When(T4.manufacturers_id = 2) then '.10'
    When(T4.manufacturers_id = 3) then '.10'
    When(T4.manufacturers_id = 4) then '0'
    When(T3.products_id = 11) then '.10'
    When(T3.products_id) between 34 and 39 then '.10'
end As Comms,  

提前致谢

1 个答案:

答案 0 :(得分:2)

case语句只返回一个值 - 遇到的第一个值。也许你想要某种连接:

CONCAT_WS(':',
          (CASE When(T4.manufacturers_id = 1) then ''
                When(T4.manufacturers_id = 2) then '.10'
                When(T4.manufacturers_id = 3) then '.10'
                When(T4.manufacturers_id = 4) then '0'
           END),
          (CASE When(T3.products_id = 11) then '.10'
                When(T3.products_id = 34) then '.10'
                When(T3.products_id = 35) then '.10'
                When(T3.products_id = 36) then '.10'
                When(T3.products_id = 37) then '.10'
                When(T3.products_id = 38) then '.10'
                When(T3.products_id = 39) then '.10'
           end) ) As Comms,