SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Union - SQL基础教程 - 笔下光年
网站首页
SQL Union
In this tutorial you will learn how to combine the results of two or more SQL queries. ## The UNION Operator The `UNION` operator is used to combine the results of two or more `SELECT` queries into a single result set. The union operation is different from using joins that combine columns from two tables. The union operation creates a new table by placing all rows from two source tables into a single result table, placing the rows on top of one another. These are basic rules for combining the result sets of two `SELECT` queries by using `UNION`: - The number and the order of the columns must be the same in all queries. - The data types of the corresponding columns must be compatible. When these criteria are met, the tables are union-compatible: ## Syntax The basic syntax of UNION can be given with: ```sql SELECT column_list FROM table1_name UNION SELECT column_list FROM table2_name; ``` To understand the union operation in a better way, let's assume that some hypothetical fields, like first_name and last_name exists in our employees and customers tables. Please note that these fields do not actually exist in our demo database tables. ```sql +----+------------+-----------+--------+ | id | first_name | last_name | salary | +----+------------+-----------+--------+ | 1 | Ethan | Hunt | 5000 | | 2 | Tony | Montana | 6500 | | 3 | Sarah | Connor | 8000 | | 4 | Rick | Deckard | 7200 | | 5 | Martin | Blank | 5600 | +----+------------+-----------+--------+ ``` Table: employees ```sql +----+------------+-----------+----------+ | id | first_name | last_name | city | +----+------------+-----------+----------+ | 1 | Maria | Anders | Berlin | | 2 | Fran | Wilson | Madrid | | 3 | Dominique | Perrier | Paris | | 4 | Martin | Blank | Turin | | 5 | Thomas | Hardy | Portland | +----+------------+-----------+----------+ ``` Table: customers Let's perform a union operation to combine the results of two queries. The following statement returns the first and last names of all the customers and employees: ```sql SELECT first_name, last_name FROM employees UNION SELECT first_name, last_name FROM customers; ``` After executing the above statement, the result set will look something like this: ```sql +---------------+--------------+ | first_name | last_name | +---------------+--------------+ | Ethan | Hunt | | Tony | Montana | | Sarah | Connor | | Rick | Deckard | | Martin | Blank | | Maria | Anders | | Fran | Wilson | | Dominique | Perrier | | Thomas | Hardy | +---------------+--------------+ ``` The `UNION` operation eliminates the duplicate rows from the combined result set, by default. That's why the above query returns only 9 rows, because if you notice the name "Martin Blank" appears in both the employees and customers tables. However, if you want to keep the duplicate rows you can use the `ALL` keyword, as follow: ```sql SELECT first_name, last_name FROM employees UNION ALL SELECT first_name, last_name FROM customers; ```
上一篇:
高级 SQL
下一篇:
SQL Like