2016-08-02 41 views
0

我建立一个视图如下里面......在MySQL中使用像一个SELECT情况下

create view usersAndTickets as 
    select tickets.subject as 'Name', 
      'automatically imported ticket from former support web' as 'Description', 
      case tickets.department_id when 3 then 'Support' when 4 then 'Support' when 5 then 'Feature Request' when 2 then 'Sales' end as 'Type', 
      case tickets.priority_id when 1 then 'Low' when 2 then 'Normal' when 3 then 'Urgent' end as 'Severity', 
      case tickets.status_id when 1 then 'Under Review' when 2 then 'Closed' when 3 then 'Waiting on Customer' when 4 then 'New' when 7 then 'New' end as 'Status', 
      users.email as 'EmailOfUserAssignedTo' 
    from custom_fields_values, tickets, users where tickets.staff_id=users.id; 

在视图中,我使用CASE WHEN ...具体的值与文本替换数字。

但我也有一个情况下,我不想做一个严格相等的比较,而是我想这样做STHG像

case when custom_fields_values.value like '%Bar%' then 'Acme Product #1' end as 'Product', 
case when custom_fields_values.value like '%Foo%' then 'Acme Product #2' end as 'Product', 

但当然,MySQL的抱怨“产品”领域已经存在。

那么我们如何使用CASE WHEN与匹配函数(LIKE)而不是相等?

+1

'CASE ...当... THEN ...当... THEN ... ELSE ... END' – MatBailie

+0

这没有用。括号似乎是必需的 –

回答

4

我觉得你只是想在case表达多个子句:

(case when custom_fields_values.value like '%Bar%' then 'Acme Product #1' 
     when custom_fields_values.value like '%Foo%' then 'Acme Product #2' 
end) as Product, 
+0

现货,谢谢。 –

相关问题