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 UNION 运算符


UNION

UNION 运算符用于组合两个或多个查询的结果集。

联合中的查询必须遵循以下规则

  • 它们必须具有相同数量的列
  • 列必须具有相同的数据类型
  • 列必须按相同的顺序排列

示例

使用 UNION 运算符组合 products testproducts

SELECT product_id, product_name
FROM products
UNION
SELECT testproduct_id, product_name
FROM testproducts
ORDER BY product_id;
运行示例 »

UNION 与 UNION ALL

使用 UNION 运算符,如果两个查询中的一些行返回完全相同的结果,则只会列出一行,因为 UNION 只选择不同的值。

使用 UNION ALL 返回重复值。

让我们对查询进行一些更改,以便在结果中包含重复值

示例 - UNION

SELECT product_id
FROM products
UNION
SELECT testproduct_id
FROM testproducts
ORDER BY product_id;
运行示例 »

示例 - UNION ALL

SELECT product_id
FROM products
UNION ALL
SELECT testproduct_id
FROM testproducts
ORDER BY product_id;
运行示例 »

×

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.