Understanding of the Union operation in SQL

Source: Internet
Author: User

In SQL, for and operations, you can use the Union keyword.

For example:

SELECT  from table_name1 UNION SELECT  from Table_name2

During the learning process, I had a question about what would happen if the property names of the two tables were different.

Directly simulating an example, the build code is as follows:

Create TableEmployee (EmpNameChar(6), Numchildrenint)Create TableDependent (DepnameChar(6), ageint)Insert  intoEmployeeValues('Haohao',2);Insert  intoEmployeeValues('Haohao',4);Insert  intoEmployeeValues('FGSD',5);Insert  intoEmployeeValues('HDS',7);Insert  intoEmployeeValues('Hauuo',9);Insert  intoEmployeeValues('Hsao',4);Insert  intoEmployeeValues('Hhao',5);Insert  intoDependentValues('Haohao',2);Insert  intoDependentValues('HDGSO',2);Insert  intoDependentValues('Hreo',2);Insert  intoDependentValues('Hjh',2);Insert  intoDependentValues('Haaao',2);

The properties of the two tables are just the same as the data types, and the test results are as follows:

(Select  empname,numchildrenfrom  employee)Union(  Select  depname,age from dependent)

The result is:

"Hjh"; 2
"Hhao"; 5
"Haohao"; 2
"HDGSO"; 2
"Haohao"; 4
"FGSD"; 5
"HDS"; 7
"Hsao"; 4
"Haaao"; 2
"Hreo"; 2
"Hauuo"; 9

As can be seen, the merger was carried out, and the duplication was removed.

Later, the SQL module in W3school saw the definition for the and operation:

Note 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. ”

Reference Link: http://www.w3school.com.cn/sql/sql_union.asp

2015-05-20

17:58:35

Understanding of the Union operation in SQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.