从postgresql中的select查询返回常量文字

时间:2016-02-15 09:04:17

标签: postgresql case

SELECT (
 CASE  
   WHEN t.status_id = 13 THEN 1
   WHEN t.status_id = 14 THEN 2 
 END)
FROM tea t WHERE t.id =13

以上查询正在运行。但我需要的是返回一个字符串而不是数字。

SELECT (
 CASE  
   WHEN t.status_id = 13 THEN CLOSE
   WHEN t.status_id = 14 THEN OPEN
 END)
FROM tea t WHERE t.id =13

1 个答案:

答案 0 :(得分:0)

@a_horse told you类似,但我建议添加一个明确的类型转换,以避免任何可能的复杂情况:

SELECT CASE t.status_id              -- "switched" case
          WHEN 13 THEN text 'CLOSE'  -- explicit cast (text would be default)
          WHEN 14 THEN      'OPEN'   -- later CASE terms are coerced to same type
                                     -- so cast is optional
       END AS status                 -- add a column alias.
FROM   tea t
WHERE  t.id = 13;