Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

PostgreSQL IN 运算符


IN

WHERE 子句中,IN 运算符允许您指定一个可能的取值列表。

IN 运算符是多个 OR 条件的简写形式。

示例

返回来自'德国'、'法国'或'英国'的所有客户

SELECT * FROM customers
WHERE country IN ('Germany', 'France', 'UK');
运行示例 »

NOT IN

IN 运算符前使用 NOT 关键字,可以返回列表中所有不包含的记录。

示例

返回所有不来自'德国'、'法国'或'英国'的客户

SELECT * FROM customers
WHERE country NOT IN ('Germany', 'France', 'UK');
运行示例 »

IN (SELECT)

您也可以在括号内使用 SELECT 语句,以返回 SELECT 语句结果中包含的所有记录。

示例

返回在 orders 表中拥有订单的所有客户

SELECT * FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders);
运行示例 »

NOT IN (SELECT)

上面示例的结果返回了 89 条记录,这意味着有 2 个客户没有下过任何订单。

让我们使用 NOT IN 运算符来检查一下是否正确。

示例

返回在 orders 表中没有下过任何订单的所有客户

SELECT * FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders);
运行示例 »

PostgreSQL 练习

通过练习测试自己

练习

选择所有 country 字段值在以下列表中的客户

('Norway', 'Sweden', 'Denmark')

SELECT * FROM customers
WHERE country ;
        

开始练习


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.