gpt4 book ai didi

sql - 如何在 postgresql 中对同一数据使用多列 IN 子句?

转载 作者:行者123 更新时间:2023-11-29 11:32:04 25 4
gpt4 key购买 nike

我有这样的查询:

SELECT c1 from t WHERE c2 IN list1 AND c3 IN list1;

我想结合这个查询得到这样的东西:

SELECT c1 from t WHERE c2 AND c3 IN list1;

最佳答案

您可以使用数组和 the operator <@ (is contained by) ,例如:

with my_table(name1, name2) as (
values ('Emily', 'Bob'), ('Ben', 'Jack'), ('Emily', 'James')
)

select *
from my_table
where array[name1, name2] <@ array['Emily', 'Jack', 'James', 'Chloe'];

name1 | name2
-------+-------
Emily | James
(1 row)

另请参阅:How to use same list twice in WHERE clause?

关于sql - 如何在 postgresql 中对同一数据使用多列 IN 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42809316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com