官术网_书友最值得收藏!

UNION

The UNION keyword is used to combine the results from multiple select queries into a single table. Each SELECT statement used in a UNION query must have the same number of columns and each column must require a similar data type. The column names from the first SELECT statement are used as the columns for the results returned.

For example, the Employee table:

For example, Contractor table:


The column names from the first SELECT statement are used as the columns for the results returned as shown in following example.

SELECT 'Employee' As Type, name, city, country FROM Employee
UNION
SELECT 'Contractor', name, city, country FROM Contractor;

The following example of UNION removes duplicate rows from the result; if we don't want to remove duplicate rows, the ALL keyword can be used:

SELECT country FROM Employee UNION SELECT country FROM Contractor;

This query returns two rows from the preceding table--Germany and Mexico. To fetch all rows from both these tables, we can use the ALL keyword as follows:

SELECT country FROM Employee UNION ALL SELECT country FROM Contractor;
主站蜘蛛池模板: 宽甸| 泗洪县| 奇台县| 邓州市| 吉木萨尔县| 桦甸市| 余姚市| 霍林郭勒市| 环江| 青州市| 正阳县| 贵州省| 古交市| 麻阳| 迭部县| 马尔康县| 长垣县| 保山市| 密山市| 嘉黎县| 九江县| 东乌珠穆沁旗| 磐安县| 宁武县| 公主岭市| 南部县| 福鼎市| 文化| 海安县| 张家港市| 化州市| 枝江市| 宁明县| 吴旗县| 伊宁县| 盐津县| 台中市| 沿河| 桃源县| 横山县| 苍溪县|