我试图根据列表中的值返回一个字符串

时间:2016-06-15 16:33:56

标签: mysql

所以我有一个ID列表,后来用一个字符串来描述这个问题。

我希望通过ID过滤掉这些内容,并根据主要ID#

显示字符串
select
sol.solution `Solution`,
count(sol.id) `Solution Count`,
case sol.id when 8,9,10,11,12,13,14,15,16,25,29,33,37,40,41,42,48,51 then 'Hardware'
else 'Software' end `HW/SW`
from tickets t
   join ticket_solutions tsol
    on tsol.ticket_id =t.id
   join solutions sol
    on tsol.solution_id = sol.id

2 个答案:

答案 0 :(得分:0)

由于您的描述不是100%明确,因此我只是猜测:

SELECT
sol.solution `Solution`,
COUNT(t.id) `Solution Count`,
IF(sol.id IN (8,9,10,11,12,13,14,15,16,25,29,33,37,40,41,42,48,51),'Hardware','Software') `HW_SW`
FROM tickets t
JOIN ticket_solutions tsol
  ON tsol.ticket_id =t.id
JOIN solutions sol
  ON tsol.solution_id = sol.id
GROUP BY sol.id

答案 1 :(得分:0)

缩短形式的case表达式不会采用值列表,只会采用单个值。但是,您可以使用带有in运算符的较长(基于条件的)表单:

CASE WHEN sol.id IN (8,9,10,11,12,13,14,15,16,25,29,33,37,40,41,42,48,51)  
     THEN 'Hardware'
     ELSE 'Software'
END AS `HW/SW`