2016-08-02 43 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

現貨,謝謝。 –

相關問題