postgreSQL - 在与任何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30263671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:54:29  来源:igfitidea点击:

postgreSQL - in vs any

postgresqloperationanysql-in

提问by PROvlima

I have tried both

我都试过了

1) smthng = any(select id from exmplTable)

1) smthng = 任何(从 exmplTable 中选择 id)

2) smthng in(select id from exmplTable)

2)smthng(选择exmplTable ID)

and I am getting the same results for my data.

我的数据得到了相同的结果。

Is there any difference for the two expresions?

这两个表达式有什么区别吗?

采纳答案by Pavel Stehule

No, in these variants are same:

不,在这些变体中是相同的:

You can see - the execution plans are same too:

您可以看到 - 执行计划也相同:

postgres=# explain select * from foo1 where id in (select id from foo2);
┌──────────────────────────────────────────────────────────────────┐
│                            QUERY PLAN                            │
╞══════════════════════════════════════════════════════════════════╡
│ Hash Semi Join  (cost=3.25..21.99 rows=100 width=4)              │
│   Hash Cond: (foo1.id = foo2.id)                                 │
│   ->  Seq Scan on foo1  (cost=0.00..15.00 rows=1000 width=4)     │
│   ->  Hash  (cost=2.00..2.00 rows=100 width=4)                   │
│         ->  Seq Scan on foo2  (cost=0.00..2.00 rows=100 width=4) │
└──────────────────────────────────────────────────────────────────┘
(5 rows)

postgres=# explain select * from foo1 where id = any (select id from foo2);
┌──────────────────────────────────────────────────────────────────┐
│                            QUERY PLAN                            │
╞══════════════════════════════════════════════════════════════════╡
│ Hash Semi Join  (cost=3.25..21.99 rows=100 width=4)              │
│   Hash Cond: (foo1.id = foo2.id)                                 │
│   ->  Seq Scan on foo1  (cost=0.00..15.00 rows=1000 width=4)     │
│   ->  Hash  (cost=2.00..2.00 rows=100 width=4)                   │
│         ->  Seq Scan on foo2  (cost=0.00..2.00 rows=100 width=4) │
└──────────────────────────────────────────────────────────────────┘
(5 rows)

回答by WigglyWorld

This may be an edge case but:

这可能是一个边缘情况,但是:

select * from myTable where id IN ()

will produce: ERROR: syntax error at or near ")"

将产生:错误:“)”处或附近的语法错误

but

select * from myTable where id = ANY('{}');

Will return an empty result set

将返回一个空的结果集