第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

使用mysql的union來合并結(jié)果集

標(biāo)簽:
MySQL

Summaryin this tutorial, you will learn how to use MySQL UNION operator to combine two or more result sets from multiple SELECT statements into a single result set.

MySQL UNION Operator

MySQL UNION operator 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 SELECT statement must have the same data type or at least convertible data type.

By default, the UNION operator eliminates duplicate rows from the result even if you don’t use DISTINCT operator explicitly. Therefore it is said that UNION clause 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 UNION to get a better understanding.

Suppose you want to combine data from the  customers and employees tables into a single result set, you can UNION operator as the following query:

SELECT customerNumber id, contactLastname name FROM customers UNION SELECT employeeNumber id,firstname name FROM employees

Here is the output:

MySQL UNION Result Set

MySQL UNION without Alias

In the example above, we used the column alias for each column in the SELECT statements. What would be the output if we didn’t use the column alias? MySQL uses the names of columns in the first SELECT statement 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 customerNumber and contactLastname as the labelwhich are the names of columns in the first SELECT statement.

MySQL Union without Column Alias

 MySQL UNION with ORDER BY

If you want to sort the results returned from the query using the UNION operator, you need to use ORDER BY clause in the last SQL SELECT statement. You can put each SELECT statement 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

MySQL UNION with ORDER BY

In the query above, first we combine id and name of both employees and customers into one result set using the UNION operator. Then we sort the result set by using the ORDER BY clause. Notice that we put the SELECT statements inside the parentheses and place the ORDER BY clause as the last statement.

If you place the ORDER BY clause in each SELECT statement, it will not affect the order of the rows in the final result produced by the UNION operator.

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

原文链接:http://outofmemory.cn/mysql/sql-union-mysql

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消