SQL Union Usage
SQL Union and SQL UNION ALL usage
SQL UNION operator
- The
-
UNION operator is used to combine the result set of two or more SELECT statements.
Notice that the SELECT statement inside the UNION must have the same number of columns. The column must also have a similar data type. Also, the order of the columns in each SELECT statement must be the same.
SQL UNION Syntax Select column_name (s) from Table_name1unionselect column_name (s) from table_name2
Comment: By default, The Union operator chooses a different value, that is, the Union is going to be heavy. If duplicate values are allowed, use UNION all.
SQL UNION All syntax select COLUMN_NAME (s) from Table_name1union Allselect column_name (s) from table_name2
Additionally, The column name in the union result set is always equal to the column name in the first SELECT statement in the Union. The purpose of the
union directive is to combine the results of two SQL statements. From this point of view, union is somewhat similar to JOIN, as these two instructions can be retrieved from multiple tables. UNION only shows two results together, not the syntax for joining two tables ............ union :
650) this.width=650; "src="/HTTP/ c.hiphotos.baidu.com/exp/w=500/sign=da65f59da918972ba33a00cad6cc7b9d/ F703738da9773912b9684ce8fa198618377ae2e9.jpg "class=" Exp-image-default "alt=" SQL Union usage "/>
And we want to find out all the days that have turnover (sales). To achieve this, we use the following SQL statement: SELECT date from store_informationunionselect date from internet_sales result:
650) this.width=650; "Src=" http://d.hiphotos.baidu.com/exp/w=500/sign=1cc5f39d40a7d933bfa8e4739d4ad194/ Dbb44aed2e738bd4a06a7bb8a38b87d6267ff9f4.jpg "class=" Exp-image-default "alt=" SQL Union usage "/>
One thing to note is that if we use "Select DISTINCT Date" in any SQL statement (or both), we will get exactly the same result.
This article is from the "11100713" blog, please be sure to keep this source http://11110713.blog.51cto.com/11100713/1834136
SQL Union and SQL UNION ALL usage