Advanced 9: joint inquiry

# Advanced 9: Union query
/ *
of Union joint merger: the results of multiple queries combined into a single result

Syntax:
query 1
of Union
query 2
of Union
...


Scenario:
The results for the query from multiple tables, multiple tables and there is no direct connection relationship, but when the information is consistent query

Features: ★
queries column 1, requires a lot of queries is the same!
Type and order of each column 2, requires multiple queries that are consistent with the query statement
3, union default keyword to weight, if the union all may contain duplicates

*/


Case # introduced: Query department number> 90 or a mailbox contains employee information

SELECT * FROM employees WHERE email LIKE '%a%' OR department_id>90;;

SELECT * FROM employees WHERE email LIKE '%a%'
UNION
SELECT * FROM employees WHERE department_id>90;


Case #: Query user information user information in Chinese men and foreign users of middle-aged men

SELECT id,cname FROM t_ca WHERE csex='男'
UNION ALL
SELECT t_id,tname FROM t_ua WHERE tGender='male';

 

Guess you like

Origin www.cnblogs.com/Diyo/p/11360215.html
Recommended