使用CASE的SQL查询

时间:2018-03-16 07:32:24

标签: mysql

Hi need query to get the email1Opened and email1Sent i.e yes/no for email_type 1 in a single row. Not on multiple rows.

我正在写一个查询来获取email1Opened和email1Sent,即yes / no,对于一行中的email_type 1。不在多行上。所以,请你帮我一行打印。

select user_id, 
 business_id, 
 email_status,
 id, email_type,
 case when email_status='open' and email_type = 1 then 'YES' else 'NO' end email1Opened,
       case when email_status='Sent' and email_type = 1 then 'YES' else 'NO' end email1Sent
from email_track where business_id = 10;

1 个答案:

答案 0 :(得分:0)

如果您只想获得email_type 1,则应在查询中使用其他位置:

select user_id, 
 business_id, 
 email_status,
 id, email_type,
 case when email_status='open' then 'YES' else 'NO' end as email1Opened,
 case when email_status='Sent' then 'YES' else 'NO' end as email1Sent
from email_track where business_id = 10 and email_type = 1;