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