在SELECT - MySQL中组合2个CASE语句

时间:2018-03-18 15:07:10

标签: mysql select case

我现在正在使用这个sql块

SELECT mail_id,
  CASE consignor
    WHEN 'aby' THEN 
      'con'
    ELSE 
     'rec'
  END AS type,

CASE consignor
  WHEN 'aby' THEN 
    recipient
  ELSE 
    consignor
END AS other,

mail, mail_time 
FROM mail m
WHERE m.consignor = user OR m.recipient = user
ORDER BY mail_time DESC

正如你所看到的,两个案例陈述完全相同,所以我认为我可以以某种方式将它们结合起来。然而,我的尝试没有产生任何结果。谁知道这里有更好的东西?

我尝试了什么?

SELECT mail_id,
  CASE consignor
    WHEN 'aby' THEN 
      'con', recipient
    ELSE 
      'rec', consignor
  END AS ( type, other ),

mail, mail_time 
FROM mail m
WHERE m.consignor = user OR m.recipient = user
ORDER BY mail_time DESC

0 个答案:

没有答案