The difference between Mysql join adding multiple conditions and where

I recently encountered a problem in the project, and I feel a little interesting. After solving the problem and consulting the relevant information, I plan to write an article to share with my friends.

Problem phenomenon:

The problem is a very common null pointer problem. When the backend queries the database data and traverses for related business processing, a null pointer is reported. Through breakpoint debugging, it is found that the problem lies in the data returned from the query, and there is an empty data in the returned List collection.
insert image description here
Just wondering, how could it return empty data?

Continue to troubleshoot

It is found that there is indeed a row of empty data after the sql is executed. Let me show the sql to the brothers first (the business code is not convenient to disclose here, it is simplified, mainly to reproduce the problem):

SELECT
	o.order_id,
	o.order_no,
	o.detail_id,
	o.product_id,
	o.batch,
	o.comp_brand_id,
	o.supplier_id,
	o.buyer
FROM
	tr_production_purchase_contract_and_order contractOrder
	LEFT JOIN t_daily_purchase_order o ON contractOrder.order_id = o.order_id 
	AND contractOrder.contract_id = '1585249657636917248' 
	AND contractOrder.del_flag = '0'

It probably means to search for the data of contract_id = '1585249657636917248', and the execution result shows that the condition contractOrder.contract_id = '1585249657636917248' AND contractOrder.del_flag = '0' does not take effect.
insert image description here
Why doesn't it take effect? The first reaction was wrong, but there is no problem after careful inspection. So I wonder if there is anything special about adding conditions after left join on. After checking the information, it is indeed very special. Adding conditions after join on is different from where conditions.

Give the conclusion first:

  • The conditional filter behind left join
    on is to filter the full connection (Cartesian product) temporary table generated by the two tables. Regardless of whether the condition after on is satisfied, all the data in the left table will be returned, and the values ​​of the right table that do not meet the conditions will be returned. is null

  • The right join
    is the same as above, except that all the data in the right table is finally returned, and the values ​​of the unqualified left table are all null

  • Inner join
    Inner join is a bit different, it is the intersection of two tables, the final result is a value that meets all conditions, so the conditions after on can take effect

  • The results of the where
    query will be filtered by the where condition at the end, and will only be returned if the condition is met.

It can be seen that during left join, it is useless to add filter conditions to the data in the left table after on. The reason for the above problem is also found because only the value of the right table is queried, because the condition is not met, so the data returned by the query is empty.

In order to verify this point of view, let's conduct a test:
employee table
insert image description here
department table
insert image description here
left join single condition query

select 
t.emp_id,
t.name,
t.age,
d.dept_id,
d.dept_name
from t_emp t
left join t_dept d on t.dept_id = d.dept_id

insert image description here
add condition after on

select 
t.emp_id,
t.name,
t.age,
d.dept_id,
d.dept_name
from t_emp t
left join t_dept d on t.dept_id = d.dept_id and t.emp_id = '4';

insert image description here
Use all the conditions of on as matching conditions, and the right table that does not match is null

add conditions after where

select 
t.emp_id,
t.name,
t.age,
d.dept_id,
d.dept_name
from t_emp t
left join t_dept d on t.dept_id = d.dept_id where t.emp_id = '4';

insert image description here
After matching and then filtering, the result is only one record.

inner join multi-condition query

select 
t.emp_id,
t.name,
t.age,
d.dept_id,
d.dept_name
from t_emp t
inner join t_dept d on t.dept_id = d.dept_id where t.emp_id = '4';

insert image description here
Summarize:

When connecting tables, in fact, the full connection of 2 tables is first performed (Cartesian product, that is, all combinable cases a.rowCount*b.rowCount), and then filtered according to the conditions behind on, and finally if it is Left join or right join, and then complete the data in the left table or right table.

Personal understanding is not necessarily correct, welcome to correct

Reference article:
Understanding of join on multiple conditions

Guess you like

Origin blog.csdn.net/weixin_44863237/article/details/132488280