微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL9.5.9学习篇布尔类型操作符select查询

介绍:

布尔类型的操作符:逻辑操作符和比较操作符

逻辑操作符:and,or,not

需要注意的是:false and null结果为false

比较运算符:is

is true

is false

is not true

is not false

is unkNown

is not unkNown

is null

is not null


登陆测试数据库创建测试测试表插入数据来演示:

1.登陆库testdb1:

[postgres@localhost ~]$ psql -Utestwjw -h 127.0.0.1 -d testdb1 -p 36985

Password for user testwjw: 输入密码:558996

psql.bin (9.5.9)

Type "help" for help.

2.创建表:

boolean的状态要么是true要么是false,如果是unkNown,用NULL表示。

boolean在sql中可以用不带引号的TRUE和FALSE表示,也可以用更多的表示真假的带引号的字符表示,如'true','false','yes','no','1','0'等

testdb1=> create table t (id int,nan boolean,note text);

CREATE TABLE

3.查看库中所有的表:

testdb1=> \dt

List of relations

Schema | Name | Type | Owner

--------+-------+-------+---------

public | t | table | testwjw

public | tlb01 | table | testwjw


testdb1=>

4.t表中插入数据:

testdb1=> insert into t values(1,TRUE,'TRUE');

INSERT 0 1

testdb1=> insert into t values(2,FALSE,'FALSE');

INSERT 0 1

testdb1=> insert into t values(3,tRue,'tRue')

testdb1-> ;

INSERT 0 1

testdb1=> insert into t values(4,fAlse,'fAlse');

INSERT 0 1

给t表中插入空数值NULL:

testdb1=> insert into t values(11,null,'null');

INSERT 0 1

testdb1=> insert into t values(11,NULL,'NULL');

INSERT 0 1

5.查看表中的数值:

testdb1=> select * from t;

id | nan | note

----+-----+-------

1 | t | TRUE

2 | f | FALSE

3 | t | tRue

4 | f | fAlse

11 | | null

11 | | NULL

(6 rows)

select * from t where nan='t';

id | nan | note

----+-----+------

1 | t | TRUE

3 | t | tRue

(2 rows)

testdb1=> select * from t where nan; 特殊的查询方式:

id | nan | note

----+-----+------

1 | t | TRUE

3 | t | tRue

(2 rows)

testdb1=>

testdb1=> select * from t where nan<>'t';

id | nan | note

----+-----+-------

2 | f | FALSE

4 | f | fAlse

(2 rows)

testdb1=>

testdb1=> select * from t where not nan;

id | nan | note

----+-----+-------

2 | f | FALSE

4 | f | fAlse

(2 rows)

testdb1=>

testdb1=> select * from t where nan or not nan;

id | nan | note

----+-----+-------

1 | t | TRUE

2 | f | FALSE

3 | t | tRue

4 | f | fAlse

(4 rows)

testdb1=> select * from t where nan and not nan;

id | nan | note

----+-----+------

(0 rows)


testdb1=> select * from t where nan is null;

id | nan | note

----+-----+------

11 | | null

11 | | NULL

(2 rows)


testdb1=> select * from t where nan is unkNown; ####神奇

id | nan | note

----+-----+------

11 | | null

11 | | NULL

(2 rows)

testdb1=>

testdb1=> select * from t where nan is not null;

id | nan | note

----+-----+-------

1 | t | TRUE

2 | f | FALSE

3 | t | tRue

4 | f | fAlse

(4 rows)

testdb1=>

插入数值:

testdb1=> insert into t values(7,'t','''t''');

INSERT 0 1

testdb1=> insert into t values(8,'f','''f''');

INSERT 0 1

testdb1=> insert into t values(9,'''yes''');

INSERT 0 1

testdb1=> insert into t values(10,'0','''0''');

INSERT 0 1

testdb1=> select * from t;

id | nan | note

----+-----+-------

1 | t | TRUE

2 | f | FALSE

3 | t | tRue

4 | f | fAlse

11 | | null

11 | | NULL

7 | t | 't'

8 | f | 'f'

9 | t | 'yes'

10 | f | '0'

(10 rows)


testdb1=> select * from t where nan is not true ;

id | nan | note

----+-----+-------

2 | f | FALSE

4 | f | fAlse

11 | | null

11 | | NULL

8 | f | 'f'

10 | f | '0'

(6 rows)


testdb1=> select * from t where nan is true ;

id | nan | note

----+-----+-------

1 | t | TRUE

3 | t | tRue

7 | t | 't'

9 | t | 'yes'

(4 rows)


testdb1=> select * from t where nan is not false ;

id | nan | note

----+-----+-------

1 | t | TRUE

3 | t | tRue

11 | | null

11 | | NULL

7 | t | 't'

9 | t | 'yes'

(6 rows)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果是个PostGreSQL的数据,于是装数据库,但这个也不懂呀,而且本系统用的Oracle,于是得解决迁移转换的问题。 总结下来两个思路。1、PostgresToOracle多金的
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么查看PostgreSQL数据库中所有表”文章能帮助大...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正确。  pg_hba.conf文件在Postgre安装文件目录下的data文件夹中。解决方案1、进入到data目录下,找到pg_hba.conf文件
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误
wamp 环境 这个提示就是说你的版本低于10了。 先打印php_info(),查看自己的版本(我这边是已经处理后的,之前的忘记截图了)
psycopg2.OperationalError: SSL SYSCALL error: EOF detected 问题提示:exception psycopg2.OperationalError
项目 postgres 连接不上, 所有连接报错 :psql: FATAL: sorry, too many clients already问题原由程序使用连接未及时释放, 连接一直处于 idle 状态处理方式1、 程序里面未释放的连接, 在使用后及时释放
服务器在同一个数据目录上启动了两个PostgreSQL实例(它已经删除postmaster.pid并使用了新的端口号,因此绕过了这种行为的正常保护措施被绕过),导致PostgreSQL的误操作postgresql 报错 FATAL: the database system is
问题原因:数据库崩溃,内存不足造成 或者 数据已损坏,磁盘故障造成首先介绍一下背景,在测试Deepgreen(Greenplum升级版)数据库时,pgbench并发数设置过多,导致数据库卡死了,在进行连接、重启、关闭时,都报同样
第 11 届 PostgreSQL 中国技术大会于 2022 年 1 月 7 日至 9 日在武汉光谷会展酒店成功举办。作为 PostgreSQL 技术领域的年度盛事,postgreSQL 中文社区旨在搭建开放、合作共享的平台,基于开源,创新驱动,共同探讨数据库行业数字化发展方向和未来新机遇。