布尔值类型 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

布尔值类型

使用 BOOLEAN 数据类型在单字节列中存储 true 和 false 值。下表描述了布尔值的三种可能状态以及导致这些状态的文本值。不管输入字符串如何,Boolean 列将存储和输出“t”表示 true,“f”表示 false。

有效的文本值 存储
True TRUE 't' 'true' 'y' 'yes' '1' 1 字节
False FALSE 'f' 'false' 'n' 'no' '0' 1 字节
Unknown NULL 1 字节

您可以使用 IS 比较将布尔值仅作为 WHERE 子句中的谓词进行检查。不能将 IS 比较与 SELECT 列表中的布尔值一起使用。

示例

您可使用 BOOLEAN 列将每个客户的“有效/无效”状态存储在 CUSTOMER 表中。

create table customer( custid int, active_flag boolean default true);
insert into customer values(100, default);
select * from customer; custid | active_flag -------+-------------- 100 | t

如果未在 CREATE TABLE 语句中指定默认值(truefalse),则插入默认值意味着插入 null。

在此示例中,查询从 USERS 表中选择喜欢运动而不喜欢电影院的用户:

select firstname, lastname, likesports, liketheatre from users where likesports is true and liketheatre is false order by userid limit 10; firstname | lastname | likesports | liketheatre ----------+------------+------------+------------- Lars | Ratliff | t | f Mufutau | Watkins | t | f Scarlett | Mayer | t | f Shafira | Glenn | t | f Winifred | Cherry | t | f Chase | Lamb | t | f Liberty | Ellison | t | f Aladdin | Haney | t | f Tashya | Michael | t | f Lucian | Montgomery | t | f (10 rows)

以下示例从 USERS 表中选择不清楚是否喜欢摇滚音乐的用户。

select firstname, lastname, likerock from users where likerock is unknown order by userid limit 10; firstname | lastname | likerock ----------+----------+---------- Rafael | Taylor | Vladimir | Humphrey | Barry | Roy | Tamekah | Juarez | Mufutau | Watkins | Naida | Calderon | Anika | Huff | Bruce | Beck | Mallory | Farrell | Scarlett | Mayer | (10 rows)

以下示例返回错误,因为它在 SELECT 列表中使用了 IS 比较。

select firstname, lastname, likerock is true as "check" from users order by userid limit 10; [Amazon](500310) Invalid operation: Not implemented

以下示例成功,因为它在 SELECT 列表中使用了等于比较 ( = ) 而不是 IS 比较。

select firstname, lastname, likerock = true as "check" from users order by userid limit 10; firstname | lastname | check ----------+-----------+------ Rafael | Taylor | Vladimir | Humphrey | Lars | Ratliff | true Barry | Roy | Reagan | Hodge | true Victor | Hernandez | true Tamekah | Juarez | Colton | Roy | false Mufutau | Watkins | Naida | Calderon |