MySql的Pass或Fail Case语句

时间:2011-12-15 19:06:42

标签: mysql case

我使用Toad for MySQL并尝试创建一个查询,告诉我返回的记录是否在任何类别中通过或失败,以便我可以告诉用户更正它们。

我当前查询的结果如下:

ID uat_resource风险培训load_performance pay_associated 1839年
1846 John Flood无无无 1849 Matt Burr N / A Matt Burr N / A N / A

我想设置它,以便当任何东西为Null时,它被认为是失败,如果风险是n / a或无,那么它也会失败,因为总是存在风险。

SELECT cr.ID,
   cr.risks,
   cr.training,
   cr.load_performance,
   cr.pay_associated
FROM requests.requests cr
WHERE cr.id IN (SELECT di.change_request_id
                   FROM dcs_item di
                  WHERE di.current_level = 3)
   AND cr.production_dt IN ('2012/01/10','2011/01/11')
ORDER BY ID;

我试图做这样的事情......

SELECT cr.ID,
   (case when lower(cr.uat_resource) in ('','n/a','none') then
   'FAIL' else 'PASS') as Test,
   cr.risks,
   cr.training,
   cr.load_performance,
   cr.pay_associated
FROM requests.requests cr
WHERE cr.id IN (SELECT di.change_request_id
                   FROM dcs_item di
                  WHERE di.current_level = 3)
   AND cr.production_dt IN ('2012/01/10','2011/01/11')
ORDER BY ID;

1 个答案:

答案 0 :(得分:0)

您的案例陈述需要“结束”:

SELECT cr.ID,
   (case when lower(cr.uat_resource) in ('','n/a','none') then
   'FAIL' else 'PASS' END) as Test,
   cr.risks,
   cr.training,
   cr.load_performance,
   cr.pay_associated
FROM requests.requests cr
WHERE cr.id IN (SELECT di.change_request_id
                   FROM dcs_item di
                  WHERE di.current_level = 3)
   AND cr.production_dt IN ('2012/01/10','2011/01/11')
ORDER BY ID;