One SQL master table record corresponds to multiple record statements in the attached table

Source: Internet
Author: User

One SQL master table record corresponds to multiple record statements in the attached table

In this case, we will use

Union statement

SQL union syntax

Select column_name (s) from table_name1
Union
Select column_name (s) from table_name2


Join left statement

Left join keyword syntax

Select column_name (s)
From table_name1
Left join table_name2
On table_name1.column_name = table_name2.column_name


Where subquery

Select * from a B where a. id = B. id


Here is an example.

Select xxxx from main table inner join appendix on main table. xxxx = appendix. xxxx where xxxx

Instance description.


Employees_china:
E_id e_name
01 zhang, hua
02 wang, wei
03 carter, thomas
04 yang, ming

Employees_usa:
E_id e_name
01 adams, john
02 bush, george
03 carter, thomas
04 gates, bill

List all different employee names in China and the United States:

Select e_name from employees_china
Union
Select e_name from employees_usa
Result
E_name
Zhang, hua
Wang, wei
Carter, thomas
Yang, ming
Adams, john
Bush, george
Gates, bill

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.