MYSQL union method of use

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_28254699/article/details/52448696

MYSQL in UNION

UNION making table will filter out duplicate links records, so the result set in the table will link the resulting sort operation to remove duplicate records and then returns the result.

for example:

select * from table1 union select * from table2

The SQL at runtime to remove the results of the two tables, and then sort space to sort remove duplicate records, and returns the result set, if a large amount of data, then the table may cause sorted by disk.  

UNION ALL MySQL in
UNION ALL simply returns the results after the merger of the two. Thus, if the result set returned by two duplicate data, then returns the result set will contain duplicate the data.

for example:

select * from table1 union all select * from table2

NOTE: When using front UNION a select column number equal to the number of a select column
such as: table1: (ID, createDate, LASTUPDATEDATE, desc, NUM, the hashCode),
     Table2 : (id, createDate, lastUpdateDate, desc)

If you now use: select * from table1 UNION ALL select * from table2 is not successful, the database is reported: 
Error
at The Used have have the SELECT A Different Number The statements of the Columns

This is the field of two tables prompted queries are not uniform, the field content than if table1 table2's more, you can use an empty string instead


select id,createDate,lastUpdateDate,desc,num,hashCode from table1 UNION ALL select id,createDate,lastUpdateDate,desc,'','' from table2 

If there are unwanted, be sure to remember the previous query and the number of fields to be back to the same query, the query four in front of you, behind also appropriate to put four, so it will not prompt a different number of parameters mistake.UNION ALL usage in mysql - Small peaches - small peach

 

In fact, a little tinkering on it
to 'select id, createDate, desc, hasCode from table1' can choose four field

 

From the efficiency, UNION ALL UNION faster than a lot, so if you can confirm that the merger of two results set does not contain duplicate the data, then use UNION ALL.

 

If you have two tables of data to different set of queries, you can use this function to operate union all

SELECT COUNT(c.a)  FROM (
(SELECT UID a,ID,SERIAL,ParkName,CardNO,ScoreRealPay,PayFlag,PayType,Createtime FROM cp_consumption_record WHERE UID=761 AND PayFlag=1  ORDER BY Createtime DESC) UNION ALL (SELECT UID a,CpResID,CpParkID,ParkSERIAL,CarCode,Price,BusinessType,CardNO,CreateDate FROM cp_reservation WHERE UID=761 AND BusinessType IN(1,2,3) ORDER BY CreateDate DESC)
) c

这是查询结果集共有多少条数据,

如果还有查询条件,直接在c后面添加就可以,比如按照时间进行查询

SELECT c.UID,c.ScoreRealPay,c.PayFlag,c.PayType  FROM (
(SELECT UID AS UID,ID AS ID,SERIAL AS SERIAL ,ParkName AS ParkName,CardNO CardNO,ScoreRealPay ScoreRealPay,PayFlag PayFlag,PayType PayType,Createtime Createtime FROM cp_consumption_record WHERE UID=761 AND PayFlag=1  ORDER BY Createtime DESC) UNION ALL (SELECT UID a,CpResID,CpParkID,ParkSERIAL,CarCode,Price,BusinessType,CardNO,CreateDate FROM cp_reservation WHERE UID=761 AND BusinessType IN(1,2,3) ORDER BY CreateDate DESC)
) c ORDER BY Createtime DESC/ASC

Emphasize here, when you want to inquire in what kind of conditions, the conditions were added to the select sub-query, the last time in uniform or reverse the positive sequence in accordance with

Note: 
the case of default, the UNION clause does not return duplicate records To display all records to be added to the ALL option. 
The UNION operation requires the same number of the query field, however, not necessarily the same field data type. 

Guess you like

Origin blog.csdn.net/qq_28254699/article/details/52448696