Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从postgresql中的select查询返回常量文字_Postgresql_Case - Fatal编程技术网

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

从postgresql中的select查询返回常量文字,postgresql,case,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

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

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

,字符串文本/常量需要用单引号括起来:
然后“CLOSE”
-但查找表可能是更好的解决方案。另外:将大小写放在括号之间(
(case…end)
)是完全无用的。放“CLOSE”不起作用。很抱歉,它起了作用。我也试过。但现在它成功了
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;