The difference between on and where of left join in MySQL

table of Contents

1 Build a table

2 Insert data

3 test

3.1 Left join: A left join B on A.id=B.id, the number of returned records is the same as the number of records in table A, regardless of whether table A/B can match

3.2 In the left connection, the difference between on condition and where condition filtering

3.3 Left join using where...is null clause


[Subject: The difference between on and where in left join in MySQL]

1 Build a table

drop table ASM_Product if exists;
create table ASM_Product(
ID int(11) unsigned not null auto_increment comment '主键ID',
Amount int(11) unsigned default null comment '数量',
PRIMARY KEY(ID)
)ENGINE=InnoDB CHARSET=utf8mb4 COMMENT='产品表'

drop table ASM_ProductDetails if exists;
create table ASM_ProductDetails(
ID int(11) unsigned not null auto_increment comment '主键ID',
Weight int(11) unsigned default null comment '重量',
Exist int(11) unsigned default null comment '是否有库存',
PRIMARY KEY(ID)
)ENGINE=InnoDB CHARSET=utf8mb4 COMMENT='产品详细信息表'

2 Insert data

insert into ASM_Product(Amount) values (100),(200),(300),(400);
insert into ASM_ProductDetails(ID, Weight,Exist) values (2,22,0),(4,44,1),(5,55,0),(6,66,1);

 select * from ASM_Product;                        select * from ASM_ProductDetails;

    

3 test

3.1 Left join: A left join B on A.id=B.id, the number of returned records is the same as the number of records in table A, regardless of whether table A/B can match

select * from ASM_Product p left join ASM_ProductDetails pd on p.ID=pd.ID;

3.2 In the left connection, the difference between on condition and where condition filtering

"A left join B on conditional expression"  : "The condition in the on conditional expression" is used to determine how to retrieve data rows from table B. This process is the matching stage of table A and table B. If there is no row in table B that matches the "condition in the on conditional expression", an additional row of data with all columns being null will be generated. In the matching phase, the "conditions in the where clause" will not work until the end of the matching phase, and the "conditions in the where clause" will not work. It will retrieve and filter the data generated in the matching phase.

(1) The use of the ON condition determines that all data rows that meet the ON condition are retrieved from the ASM_ProductDetails table of the left join.

select * from ASM_Product p left join ASM_ProductDetails pd on p.ID=pd.ID and pd.ID=2;

(2) After the left join, use the where clause to filter out the data rows that do not meet the where conditions from the left join data.

select * from ASM_Product p left join ASM_ProductDetails pd on p.ID=pd.ID where pd.ID=2;

(3) All rows of data from the ASM_Product table have been retrieved, but no records are matched in the ASM_ProductDetails table.

select * from ASM_Product p left join ASM_ProductDetails pd on p.ID=pd.ID and p.Amount=100;

(4) All rows of data from the ASM_Product table have been retrieved, and records are also matched in the ASM_ProductDetails table.

select * from ASM_Product p left join ASM_ProductDetails pd on p.ID=pd.ID and p.Amount=200;

3.3 Left join using where...is null clause

select * from ASM_Product p left join ASM_ProductDetails pd on p.ID = pd.ID and pd.Weight != 44 and pd.Exist = 0 where pd.ID is null;

Analysis: First filter the on condition to obtain the result set of the B table, merge it into the A table, and then use the where condition to filter the result set.

select * from ASM_Product p left join ASM_ProductDetails pd on p.ID = pd.ID and pd.Weight != 44 and pd.Exist = 1 where pd.ID is null;

Analysis: First filter the on condition to obtain the result set of the B table, merge it into the A table, and then use the where condition to filter the result set.

 

Reference : https://www.oschina.net/question/89964_65912

 

Guess you like

Origin blog.csdn.net/cmm0401/article/details/109855618