一.IN && NOT IN

WHERE expression IN (subquery)

右边圆括号内是返回一个字段的子查询结果集,左边的表达式(或字段)对查询结果每一行进行一次运算和比较,如果结果集中存在相等的行,则IN结果为'TRUE',否则为'FALSE';

WHERE expression NOT IN (subquery)

NOT IN与IN正相反,如果结果集中不存在相等的行结果为'TRUE',否则为'FALSE'。

测试表:

test=# \d tbl_test
Table "public.tbl_test"
Column | Type | Modifiers
--------+---------+-----------
f | integer | test=# \d tbl_insert
Table "public.tbl_insert"
Column | Type | Modifiers
--------+-----------------------+-----------
a | integer |
b | integer |
c | character varying(12) | test=# select * from tbl_test ;
f
---
1
3
5
(3 rows) test=# select * from tbl_insert;
a | b | c
---+---+-------
1 | 1 | 11
2 | 2 | 22
3 | 3 | 33
4 | 4 | 44
5 | 5 | 51
6 | 6 | 1
6 | 6 | 61
6 | 6 | 661
7 | 7 | 3%1
8 | 8 | 3%_1
8 | 8 | 3_%_1
7 | 7 | abc
7 | 7 | ABc
7 | 7 | aBC
(14 rows)

示例1.查询tbl_insert表,且a字段值在tbl_test表字段f中的行

test=# select * from tbl_insert where a in (select f from tbl_test);
a | b | c
---+---+----
1 | 1 | 11
3 | 3 | 33
5 | 5 | 51
(3 rows)

示例2.查询tbl_insert表,且a字段值比tbl_test表字段f小1的行

test=# select * from tbl_insert where a+1 in (select f from tbl_test);
a | b | c
---+---+----
2 | 2 | 22
4 | 4 | 44
(2 rows)

示例3.查询tbl_insert表,且a字段值不在tbl_test表字段f中的行

test=# select * from tbl_insert where a not in (select f from tbl_test);
a | b | c
---+---+-------
2 | 2 | 22
4 | 4 | 44
6 | 6 | 1
6 | 6 | 61
6 | 6 | 661
7 | 7 | 3%1
8 | 8 | 3%_1
8 | 8 | 3_%_1
7 | 7 | abc
7 | 7 | ABc
7 | 7 | aBC
(11 rows)

示例4.查询tbl_insert表,且a字段值等于5或7的行

test=# select * from tbl_insert where a in (5,7);
a | b | c
---+---+-----
5 | 5 | 51
7 | 7 | 3%1
7 | 7 | abc
7 | 7 | ABc
7 | 7 | aBC
(5 rows)

二.EXISTS && NOT EXISTS

WHERE EXISTS (subquery)

括号内同样是一个子查询,如果子查询有返回结果,则EXISTS结果为'TRUE',否则为'FALSE'。

WHERE NOT EXISTS(subquery)

NOT EXISTS与EXISTS正好相反,如果子查询没有返回结果,为'TRUE',否则'FALSE'。

示例1.查询tbl_insert表,且a字段值在tbl_test表字段f中的行

test=# select * from tbl_insert where exists (select null from tbl_test where tbl_test.f=tbl_insert.a);
a | b | c
---+---+----
1 | 1 | 11
3 | 3 | 33
5 | 5 | 51
(3 rows)

示例2.查询tbl_insert表,且a字段值不在tbl_test表字段f中的行

test=# select * from tbl_insert where not exists (select null from tbl_test where tbl_test.f=tbl_insert.a);
a | b | c
---+---+-------
2 | 2 | 22
4 | 4 | 44
6 | 6 | 1
6 | 6 | 61
6 | 6 | 661
7 | 7 | 3%1
8 | 8 | 3%_1
8 | 8 | 3_%_1
7 | 7 | abc
7 | 7 | ABc
7 | 7 | aBC
(11 rows)

PS:NOT IN的效率非常低,如果可以的话建议使用NOT EXISTS。

postgresql----IN&&EXISTS的更多相关文章

  1. CONTINUOUS MIGRATION

    转自:https://pgloader.io/blog/continuous-migration/ After having been involved in many migration proje ...

  2. 【Harbor学习笔记】-教你快速搭建Docker私有仓库

    目录 架构图 Harbor依赖的外部组件 Harbor自有组件 核心组件 安装 1. 下载离线安装包 2. 配置 harbor.cfg (harbor.yml) 3. 启动 Harbor 安装配置问题 ...

  3. Postgresql:prepared statement "S_1" already exists

    近期由于业务需要和一些json的存储查询需要,把新的应用切到pgsql上来,刚刚切好,是可以正常使用的,但是偶尔会来一下 java连接pgsql 偶尔出现 这个错.   org.postgresql. ...

  4. PG cannot execute UPDATE in a read-only transaction | How to add column if not exists on PostgreSQL

    PG cannot execute UPDATE in a read-only transaction出现这种情况时,说明SQL语句可能是运行在一个PG集群中的非master节点上.查看data/pg ...

  5. Postgresql中无则插入的使用方法INSERT INTO WHERE NOT EXISTS

    一.问题 Postgresql中无则插入的使用方法INSERT INTO WHERE NOT EXISTS,用法请参考样例. 二.解决方案 (1)PostgresSQL INSERT INTO tes ...

  6. [PostgreSql]PostgreSql调用函数及用IF EXISTS判断表是否存在

    1.创建一个函数function1 -- FUNCTION: public.function1(character varying, integer) -- DROP FUNCTION public. ...

  7. postgresql 基本语法

    postgresql数据库创建/修改/删除等写入类代码语法总结: 1,创建库 2,创建/删除表 2.1 创建表 create table myTableName 2.2 如果表不存在则创建表 crea ...

  8. PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库

    最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...

  9. PostgreSql性能测试

    # PostgreSql性能测试 ## 1. 环境+ 版本:9.4.9+ 系统:OS X 10.11.5+ CPU:Core i5 2.7G+ 内存:16G+ 硬盘:256G SSD ## 2. 测试 ...

  10. PostgreSQL

    PostgreSQL新手入门   作者: 阮一峰 日期: 2013年12月22日 自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQ ...

随机推荐

  1. java中Scanner的nextLine()和next()的区别

    首先,next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后 ...

  2. Js Object转化为json,json转Object

    var obj={x:10,y:50};var t= JSON.stringify(obj);console.log(typeof t);var gg= JSON.parse(t);console.l ...

  3. preventDefault

    e.preventDefault()阻止事件默认行为 例如: $("a").click(function (e) {   alert("默认行为被禁止喽"); ...

  4. Java线程之Callable和Future

    本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果.        Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结 ...

  5. 系统管理员应该知道的20条Linux命令

    如果您的应用程序不工作,或者您希望在寻找更多信息,这 20 个命令将派上用场. 在这个全新的工具和多样化的开发环境井喷的大环境下,任何开发者和工程师都有必要学习一些基本的系统管理命令.特定的命令和工具 ...

  6. shiro+spring相关配置

    首先pom中添加所需jar包: <!-- shiro start --> <dependency> <groupId>org.apache.shiro</gr ...

  7. SQL Server死锁的解除方法

    如果想要查出SQL Server死锁的原因,下面就教您SQL Server死锁监控的语句写法,如果您对此方面感兴趣的话,不妨一看. 下面的SQL语句运行之后,便可以查找出SQLServer死锁和阻塞的 ...

  8. HEVC compressGOP 接口

    HEVC编码调用compressGOP()来实现一个GOPSize 图像序列的编码.在reference code里,真正做compressGOP编码之前,需要存GOPSize帧YUV在m_cList ...

  9. 【matlab】图像去噪的代码测试

    %% 自己设置频域的滤波窗口 girl=imread('F:\Users\*****\Pictures\CGS_stripe1.bmp'); girl=rgb2gray(girl); girl=im2 ...

  10. error C2065:!错误:未定义标识符“pBuf);”

    error C2065: “pBuf):”: 未声明的标识符 错误原因:第二个括号)使用的是中文符号!还有最后那个分号! 改回来就好了~ 原错误: 修正后错误消失: