gpt4 book ai didi

sql - 在 Select 中将 boolean 值返回为 TRUE 或 FALSE (PostgreSQL/pgAdmin)

转载 作者:行者123 更新时间:2023-11-29 11:12:03 25 4
gpt4 key购买 nike

在 PostgreSQL(版本 9.4,pgAdmin3)中,当在具有 boolean 列的表上执行选择时,数据输出显示“t”或“f”。我想在不编写 CASE 语句或执行 JOINS 等的情况下将 boolean 值转换/转换为 TRUE 或 FALSE。

顺便说一句,根据 PostgreSQL 自己的 documentation此行为不是 SQL 标准。

The key words TRUE and FALSE are the preferred (SQL-compliant) usage.

PS:只有在 pgAdmin 中使用 SQL 编辑器时才会发生这种情况。使用 pgAdmin 对象浏览器,向下钻取到同一个表,右键单击,查看数据,查看前 100 行,相同的 boolean 列显示为 TRUE 或 FALSE,如预期/标准。

最佳答案

如果您只想显示文字 TRUEFALSE,您可以像您建议的那样使用 case 语句。因为 PostgreSQL 对待 TRUE, true, yes, on, y, t1 为真,我将控制我希望输出的样子。

Where子句可以这样写:

select * from tablename where active
--or--
select * from tablename where active = true

(我的建议与 PostgreSQL 相同——使用 true)

选择时,尽管使用 case 语句可能会犹豫,但我仍然建议这样做以控制输出字符串文字。

您的查询将如下所示:

select 
case when active = TRUE then 'TRUE' else 'FALSE' end as active_status,
...other columns...
from tablename
where active = TRUE;

SQLFiddle 示例:http://sqlfiddle.com/#!15/4764d/1

create table test (id int, fullname varchar(100), active boolean);
insert into test values (1, 'test1', FALSE), (2, 'test2', TRUE), (3, 'test3', TRUE);

select
id,
fullname,
case when active = TRUE then 'TRUE' else 'FALSE' end as active_status
from test;

| id | fullname | active_status |
|----|----------|---------------|
| 1 | test1 | FALSE |
| 2 | test2 | TRUE |
| 3 | test3 | TRUE |

关于sql - 在 Select 中将 boolean 值返回为 TRUE 或 FALSE (PostgreSQL/pgAdmin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com