1. 程式人生 > >PostgreSQL9.5.9學習篇布爾類型操作符select查詢

PostgreSQL9.5.9學習篇布爾類型操作符select查詢

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:

[[email protected] ~]$ 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‘,‘‘‘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)

本文出自 “10931853” 博客,請務必保留此出處http://wujianwei.blog.51cto.com/10931853/1970348

PostgreSQL9.5.9學習篇布爾類型操作符select查詢