SQL left connection strategy-mysql learning experience (in line with SQL Standards)

Source: Internet
Author: User

SQL left Outer Join: Understand the left join and then understand the right join

Two-Table Outer Join

Shipping table
Customer ID, customer name, product ID, product quantity, remarks

Create Table a_prod_out
(
Custom_id int not null,
Custom_name varchar (200) not null,
Prod_id int default 0 null,
Prod_num int default 0 null,
Remark varchar (500) null,
Constraint pk_a_prod_out primary key (custom_id)
)

Product table
Product ID, product name, manufacturer

Create Table B _prod
(
Prod_id int not null,
Prod_name varchar (200) not null,
Manufacturer_id int default 0 null,
Remark varchar (500) null,
Constraint pk_ B _prod primary key (prod_id)
)

Left join
The left join is based on the left table, and some data on the right is connected, and the condition is the product ID.
The information in the left table is displayed, indicating that the join conditions are met on the right of the connection.

Select a. custom_id, A. custom_name, B. prod_name, B. manufacturer_id
From a_prod_out a left Outer Join B _prod B
On a. prod_id = B. prod_id;

Three-Table Outer Join

What products do the customer apply?
Create Table a_mfter
(
Manufacturer_id int not null,
Manufacturer_name varchar (200) not null,
Constraint pk_a_mfter primary key (manufacturer_id)
)

Select a. custom_id, A. custom_name, B. prod_name, C. manufacturer_name
From a_prod_out
Left Outer Join B _prod B
On a. prod_id = B. prod_id
Left Outer Join a_mfter C
On B. manufacturer_id = C. manufacturer_id;

The SQL results are also based on the shipping table in the left table, and are connected to the product table and vendor table.
 

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.