pg学习_子查询

子查询
子查询的语法
SELECT select_list FROM table
WHERE expr operator
(SELECT select_list FROM table);
子查询规则 ()最好放在右边,提高查询的速度。
子查询类型有两种:
单行子查询
多行子查询

1、单行子查询
1)单行子查询所用的操作符号 =
>
>=
< 
<=
<>
简单的单行子查询
highgo=# select * from test
highgo-# where no >(select no from test where name='adam');
组函数的子查询
highgo=# select * from test
highgo-# where no >(select max(no) from test where name='adam');
 no |  name  
----+--------
  3 | eva
  3 | lilith
(2 rows)
having 语句使用子查询
highgo=# select no,count(*) from test group by no
highgo-# having no>(select min(no) from test where name='adam');
 no | count 
----+-------
  3 |     2
  2 |     2
(2 rows)

2、多行子查询
多行子查询所用的操作符号 
in
any 大于> 大于最小
小于< 小于最大
等于= 相对于 in 
all 
大于> 大于最大 
小于< 小于最小
highgo=# select * from test
highgo-# where no in (select no from test where name='adam');
 no |  name  
----+--------
  1 | adam
  2 | lilith
  1 | lilith
  1 | eva
  2 | adam
(5 rows)

highgo=# select * from test
highgo-# where no > any (select no from test where name='adam');
 no |  name  
----+--------
  2 | lilith
  3 | eva
  2 | adam
  3 | lilith
(4 rows)

highgo=# select * from test
highgo-#  where no > all (select no from test where name='adam');
 no |  name  
----+--------
  3 | eva
  3 | lilith
(2 rows)

3、子查询作from子句
在进一步优化后的 HighGo DB 中,当子查询作为 from 的子句时允许不添加别名,这与 oracle 做到了充分的兼容。
highgo=# select * from
highgo-# (select * from test where name='adam')where no=1;
 no | name 
----+------
  1 | adam
(1 row)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值