union mysql 同比_使用mysql的union来合并结果集

Summary:in this tutorial, you will learn how to use MySQL UNIONoperator to combine two or more result sets from multiple SELECTstatements into a single result set.

MySQL UNION Operator

MySQL UNIONoperator allows you to combine two or more result sets from multiple tables into a single result set. The syntax of the MySQL UNION is as follows:SELECT column1, column2 UNION [DISTINCT | ALL] SELECT column1, column2 UNION [DISTINCT | ALL] …

There are some rules that you need to follow in order to use the UNION operator:The number of columns appears in the corresponding SELECT statements must be equal.

The columns appear in  the corresponding positions of each SELECTstatement must have the same data type or at least convertible data type.

By default, the UNIONoperator eliminates duplicate rows from the result even if you don’t use DISTINCToperator explicitly. Therefore it is said that UNIONclause is the shortcut of UNION DISTINCT.

If you use the UNION ALL explicitly, the duplicate rows, if available, remain in the result. The UNION ALL performs faster than the UNION DISTINCT.

MySQL UNION example

Let’s practice with an example of using MySQL UNIONto get a better understanding.

Suppose you want to combine data from the  customers and employeestables into a single result set, you can UNIONoperator as the following query:SELECT customerNumber id, contactLastname name FROM customers UNION SELECT employeeNumber id,firstname name FROM employees

Here is the output:

AAffA0nNPuCLAAAAAElFTkSuQmCC

MySQL UNION without Alias

In the example above, we used the column alias for each column in the SELECTstatements. What would be the output if we didn’t use the column alias? MySQL uses the names of columns in the first SELECTstatement as the labels for the output.

Let’s try the query that combines customers and employees information without using column alias:(SELECT customerNumber, contactLastname FROM customers) UNION (SELECT employeeNumber, firstname FROM employees) ORDER BY contactLastname, customerNumber

The result has customerNumberand contactLastnameas the label,which are the names of columns in the first SELECTstatement.

AAffA0nNPuCLAAAAAElFTkSuQmCC

MySQL UNION with ORDER BY

If you want to sort the results returned from the query using the UNIONoperator, you need to use ORDER BY clause in the last SQL SELECTstatement. You can put each SELECTstatement in the parentheses and use the ORDER BY clause as the last statement.

Let’s take a look at the following example:(SELECT customerNumber id,contactLastname name FROM customers) UNION (SELECT employeeNumber id,firstname name FROM employees) ORDER BY name,id

AAffA0nNPuCLAAAAAElFTkSuQmCC

In the query above, first we combine idand nameof both employees and customers into one result set using the UNIONoperator. Then we sort the result set by using the ORDER BY clause. Notice that we put the SELECTstatements inside the parentheses and place the ORDER BY clause as the last statement.

If you place the ORDER BY clause in each SELECTstatement, it will not affect the order of the rows in the final result produced by the UNIONoperator.

MySQL also provides you with alternative option to sort the result set based on column position using ORDER BY clause as the following query:(SELECT customerNumber, contactLastname FROM customers) UNION (SELECT employeeNumber,firstname FROM employees) ORDER BY 2, 1

In this tutorial, you have learned how to use MySQL UNION statement to combine data from multiple tables into a single result set.

Related Tutorials

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值