postgresql----IN&&EXISTS
一.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的更多相关文章
- CONTINUOUS MIGRATION
转自:https://pgloader.io/blog/continuous-migration/ After having been involved in many migration proje ...
- 【Harbor学习笔记】-教你快速搭建Docker私有仓库
目录 架构图 Harbor依赖的外部组件 Harbor自有组件 核心组件 安装 1. 下载离线安装包 2. 配置 harbor.cfg (harbor.yml) 3. 启动 Harbor 安装配置问题 ...
- Postgresql:prepared statement "S_1" already exists
近期由于业务需要和一些json的存储查询需要,把新的应用切到pgsql上来,刚刚切好,是可以正常使用的,但是偶尔会来一下 java连接pgsql 偶尔出现 这个错. org.postgresql. ...
- 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 ...
- Postgresql中无则插入的使用方法INSERT INTO WHERE NOT EXISTS
一.问题 Postgresql中无则插入的使用方法INSERT INTO WHERE NOT EXISTS,用法请参考样例. 二.解决方案 (1)PostgresSQL INSERT INTO tes ...
- [PostgreSql]PostgreSql调用函数及用IF EXISTS判断表是否存在
1.创建一个函数function1 -- FUNCTION: public.function1(character varying, integer) -- DROP FUNCTION public. ...
- postgresql 基本语法
postgresql数据库创建/修改/删除等写入类代码语法总结: 1,创建库 2,创建/删除表 2.1 创建表 create table myTableName 2.2 如果表不存在则创建表 crea ...
- PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库
最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...
- PostgreSql性能测试
# PostgreSql性能测试 ## 1. 环境+ 版本:9.4.9+ 系统:OS X 10.11.5+ CPU:Core i5 2.7G+ 内存:16G+ 硬盘:256G SSD ## 2. 测试 ...
- PostgreSQL
PostgreSQL新手入门 作者: 阮一峰 日期: 2013年12月22日 自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQ ...
随机推荐
- mac for appium环境安装
之前写过windows 安装appium环境步骤. 1. 需求的前置条件如下 (mac 自动git.ruby.brew命令): 2. java 环境 3. git 环境 4. ruby环境 5. b ...
- Hibernate中createCriteria即QBC查询的详细用法
现在假设有一个Student类,内有id,name,age属性String hql = "from Student s";按照以前的做法,我们通常是Query query = se ...
- 初识EseNt
转自:http://www.cnblogs.com/goosao/archive/2011/09/23/2186412.html 一.什么是EseNtEseNt(Extensible Storage ...
- 详解JNDI的lookup资源引用 java:/comp/env
ENC的概念: The application component environment is referred to as the ENC, the enterprise naming c ...
- 【转载】C#进阶系列——动态Lamada
前言:在DDD系列文章里面,我们在后台仓储里面封装了传递Lamada表达式的通用方法,类似这样: public virtual IQueryable<TEntity> Find(Expre ...
- erlang在NotePad++下的高亮
转自:http://www.roberthorvick.com/2009/07/08/syntax-highlighing-for-erlang-in-notepad/ Syntax Highligh ...
- 清理SYSAUX表空间的WRH$_LATCH_CHILDREN表
周六 被突然起来的短信 轰醒. 一看有63条短信. 都是来之与监控中的.有关表空间大小超过某个警戒值. 发现 SYSAUX表空间超过了15GB 通过以下代码查看SYSAUX表空间的功能占用情况 SEL ...
- unicode and utf-8
今晚听同事分享提到这个,简单总结下. Unicode字符集 Unicode的出现是因为ASCII等其他编码码不够用了,比如ASCII是英语为母语的人发明的,只要一个字节8位就能够表示26个英文字母了, ...
- 提取Unity游戏资源和脚本
UnityStudio UnityStudio可以直接在自己的软件上查看图片.shader.文本.还能直接播放音频.甚至还能看场景Hierarchy视图的树状结构.强烈推荐用UnityStudio. ...
- c# 以二进制读取文本文件
using System; using System.IO; public class FileApp { public static void Main() { // ...