一.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. C++ 数据抽象

    C++ 数据抽象数据抽象是指,只向外界提供关键信息,并隐藏其后台的实现细节,即只表现必要的信息而不呈现细节. 数据抽象是一种依赖于接口和实现分离的编程(设计)技术. 让我们举一个现实生活中的真实例子, ...

  2. cutadapt 的安装与使用

    cutadapt 是一款质量过滤的软件, 它可以删除adapter, primer. polyA尾等序列:也可以用来去除低质量序列 源代码: https://github.com/marcelm/cu ...

  3. Linux新手要了解的十个知识点

    Linux对于有的新手来说,感觉无从下手,或者不知道从哪儿学起?怎么学?针对这些问题,我给大家说说新手学习Linux需要了解的十个知识点. 注意大小写 Linux是大小写敏感的系统,举个例子,Mozi ...

  4. shell脚本程序中循环、判断语句的介绍

    shell的循环主要有3种,for,while,until shell的分支判断主要有2种,if,case 一,for循环 C/C++ Code复制内容到剪贴板 #!/bin/bash for fil ...

  5. js 停止事件冒泡 阻止浏览器的默认行为

    在前端开发工作中,由于浏览器兼容性等问题,我们会经常用到“停止事件冒泡”和“阻止浏览器默认行为”. 浏览器默认行为: 在form中按回车键就会提交表单:单击鼠标右键就会弹出context menu. ...

  6. Ubuntu 14.04 Server i386 安装 Oracle11g_11.2.0.3 RAC

    文档地址:doc 文档地址:doc

  7. CMD命令进入文件夹

    cmd 进入E文件夹 E: 查看文件夹目录  dir 进入某个文件夹 cd 目录

  8. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(一)

    题外话:本篇是对之前那篇的重排版.并拆分成两篇,免得没了看的兴趣. 前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的 ...

  9. Codeforces Round #313 D. Equivalent Strings(DFS)

    D. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  10. UVa 10450 - World Cup Noise

    题目:构造一个01串,使得当中的1不相邻,问长度为n的串有多少中. 分析:数学,递推数列. 设长度为n的串有n个.则有递推关系:f(n)= f(n-1)+ f(n-2): 长度为n的结束可能是0或者1 ...