使用case when then else end 的注意点

SELECT
   case
    WHEN LOCATE('nocheck', t.A) > 0 && t.A is null || t.A = ''
    THEN t.op
   ELSE t.A
  END AS total ,
t.op,
t.A
 FROM (
SELECT 
a.value as A   ,
c.value  AS C  ,
ee.`value` AS EE ,
c.value +  ee. value as op
FROM
  ar_approve_entry_result a
INNER JOIN ar_approve_entry_result c ON a.approve_result_uuid = c.approve_result_uuid
AND c.entry_UUID = 'AREN17313'
INNER JOIN ar_approve_entry_result ee on c.approve_result_uuid = ee.approve_result_uuid
and ee.entry_UUID = 'AREN17314'
INNER JOIN (
  SELECT
    b.uuid,
    b.pay_comfirm_uuid
  FROM
    ar_approve_result b
  WHERE
    b.sys_status = 1
  AND b.pay_comfirm_uuid = '168a9110776443ee9d9500a92be93a61'
  ORDER BY
    b.db_create_sys_time DESC
  LIMIT 0,1
) b ON a.approve_result_uuid = b.uuid
WHERE
  a.sys_status = 1
AND a.entry_UUID = 'AREN17292'
) as  t 
SELECT
 a.value,
 SUM(b.`value`),
  CASE
    WHEN LOCATE('nocheck', a.VALUE) > 0 && a.`value` is null || a.`value` = ''
    THEN SUM(b.`value`)
   ELSE a.value
  END AS total
FROM
 ar_approve_entry_result b
INNER JOIN ar_approve_entry_result a ON a.approve_result_uuid = b.approve_result_uuid
AND a.entry_UUID = 'AREN17292'
WHERE
 b.approve_result_uuid = (
  SELECT
   uuid
  FROM
   ar_approve_result
  WHERE
   pay_comfirm_uuid = '168a9110776443ee9d9500a92be93a61'
  AND sys_status = 1
  ORDER BY
   datetime DESC
  LIMIT 0,
  1
 )
AND b.entry_UUID IN ('AREN17313', 'AREN17314')

locate(subStr,string) :函数返回subStr在string中出现的位置
  1. // 如果字符串 string 包含 subStr
  2. locate(subStr,string) > 0
    1. // 如果字符串 string 不包含 subStr
    2. locate(subStr,string) = 0
       在使用 case when    then  语句的时候:
      注意1: then 后面的字段,只能直接获得 , 不能通过 间接获得,比如上面的 一个相加的值,要想那些写,只能在 包一层,才能获得使用
原文地址:https://www.cnblogs.com/xiaoniuniu886/p/11908502.html