Wu Yuxiong - natural born MySQL study notes: MySQL UNION operator

Results MySQL UNION operator SELECT statement which is connected to two or more combinations of a result set. Multiple SELECT statement removes duplicate data. 
Syntax 
MySQL UNION operator syntax: 
the SELECT expression1, expression2, ... expression_n 
the FROM Tables 
[the WHERE Conditions] 
the UNION [ALL | the DISTINCT] 
the SELECT expression1, expression2, ... expression_n 
the FROM Tables 
[the WHERE Conditions]; 
Parameters 
expression1, expression2, ... expression_n: to retrieve the column. 
tables: to retrieve the data table. 
WHERE conditions: optional search condition. 
DISTINCT: Optional, delete duplicate the results of the data set. By default, the UNION operator has removed the duplicate data, so the impact on the results DISTINCT modifier nothing. 
ALL: Alternatively, return all the result sets that contain duplicate data.
The following is a selected " Websites " table of data: 
MySQL > the SELECT * the FROM Websites; 
MySQL > the SELECT * the FROM Apps;
The following SQL statement from " Websites " and " Apps " Select Country all the different (different values only) in the table: 
the SELECT Country the FROM Websites 
the UNION 
the SELECT Country the FROM Apps 
the ORDER BY Country; 
NOTE: UNION lists two tables can not be used All of the country. If some of the sites and APP from the same country, each country is listed only once. UNION only select a different value. Use UNION ALL to select the duplicate values!
SQL UNION ALL Examples 
The following SQL statements from UNION ALL " Websites " and " Apps " table select all Country (also duplicate values): 
the SELECT Country Websites the FROM 
UNION ALL 
the SELECT Country the FROM Apps 
the ORDER BY Country;
The following SQL statement using UNION ALL from " Websites " and " Apps " select all China (CN) in the data table (also duplicate values): 
the SELECT Country, name the FROM Websites 
the WHERE Country = ' the CN ' 
UNION ALL 
the SELECT Country, Apps the FROM APP_NAME 
the WHERE Country = ' the CN ' 
the ORDER BY Country;

 

Guess you like

Origin www.cnblogs.com/tszr/p/12113779.html