如何从 Postgres 中的字符串类型字段返回 bool 值 true/false 而不是 t/f。
SELECT (CASE WHEN status = 'active' THEN true ELSE false END)::boolean AS status
FROM table;
最佳答案
t
和 f
只是 psql
打印 bool 值的方式,它应该不重要。此外,您还可以按如下方式简化查询:
SELECT status = 'active' AS status FROM table;
如果您确实想获取status
列的文本,请执行以下操作:
SELECT (status = 'active')::text AS status FROM table;
如果您想获取一个数字(0 表示 false,1 表示 true),请执行以下操作:
SELECT (status = 'active')::integer AS status FROM table;
关于postgresql - 使用 Postgres 返回 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39570341/