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 ...
随机推荐
- 空间管理 您的位置: 51Testing软件测试网 » lilisx2006的个人空间 » 日志 在一个没有测试经理的小公司如何做好测试
如何在一个没有测试经理的小公司做好测试? 首先,没有测试经理意味着测试人员没有最直接的管理者,往往这种时候的管理者是开发经理或技术总监,但他们何其忙耶?同时,在无人监管的情况下,测试是一个很容易偷懒的 ...
- openfire数据库mysql配置
<?php return array( //'配置项'=>'配置值' //'USERNAME'=>'admin', //赋值 //数据库配置信息 'DB_TYPE' => 'm ...
- R语言低级绘图函数-points
points 用来在一张图表上添加点,指定好对应的x和y坐标,就可以添加不同形状,颜色的点了: 基本用法: 通过x和y设置点的坐标 plot(1:5, 1:5, xlim = c(0,6), ylim ...
- devstack install attributeError: 'module' object has no attribute '__version__'
work around: edit the file /usr/local/lib/python2.7/dist-packages/openstack/session.py and remove th ...
- 【Java面试题】31 介绍Collection框架的结构
Collection:List列表,Set集 Map:Hashtable,HashMap,TreeMap Collection 是单列集合 List 元素是有序的.可重复 有序的 colle ...
- 基于Nginx反向代理及负载均衡
基于Nginx反向代理及负载均衡 参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 只要没有被启用,默认就是 ...
- C# 基础小知识之yield 关键字
对于yield关键字我们首先看一下msdn的解释: 如果你在语句中使用 yield 关键字,则意味着它在其中出现的方法.运算符或 get 访问器是迭代器. 通过使用 yield 定义迭代器,可在实现自 ...
- IOS -执行时 (消息传递 )
一 函数调用概述 Objective-C不支持多重继承(同Java和Smalltalk),而C++语言支持多重继承. Objective-C是动态绑定,它的类库比C++要easy操作. Ob ...
- Maven------pom.xml自动加载各种类库代码
转载: http://lavasoft.blog.51cto.com/62575/1388866/ 一般要加<type>jar</type> <dependency> ...
- Extjs学习笔记--(四,基本函数介绍)
Ext是Extjs的命名空间,为Extjs框架提供唯一的全局变量 这样做可以避免冲突,便于代码维护 1,apply和applyif方法 apply=function(object, config, d ...