Detailed analysis of the problem of adding where conditions using left join in MySQL

Detailed analysis of the problem of adding where conditions using left join in MySQL

Current demand:

There are two tables, group and factor. One group corresponds to multiple factors. Now we want to query the valid groups and the corresponding valid factors . Both tables have the isDel logical deletion flag.

insert image description here

The first mistake :

SELECT g.*,f.* FROM groups g LEFT JOIN factor f ON f.groupId = g.id where g.isDel=0 and f.isDel=0

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matching rows in the right table (table_name2).
If the table on the left does not have corresponding data in the table on the right, the query cannot be done because of the code problem of f.isDel=0 .

Wrong way of writing:

SELECT g.*,f.* FROM groups g LEFT JOIN factor f ON f.groupId = g.id and g.isDel=0 and f.isDel=0

This way of writing g.isDel=0 will not take effect, resulting in incorrect query data.

Cause Analysis:

Several knowledge points about where and on conditions in left join:

  • Multi-table left join will generate a temporary table and return it to the user
  • The where condition is to filter the last generated temporary table and filter out the records that do not meet the where condition.
  • The on condition is to conditionally filter the right table of the left join, but all rows of the left table are still returned, and those that are not in the right table are filled with NULL
  • If there are restrictions on the left table in the on condition, all rows of the left table will be returned regardless of whether the condition is true or false, but the matching values ​​of the right table will be affected. That is to say, the restriction conditions of the left table in on only affect the matching content of the right table, and do not affect the number of returned rows.

Correct way to write it:

SELECT g.*,f.* FROM groups g LEFT JOIN factor f ON f.groupId = g.id and f.isDel=0 where g.isDel=0

in conclusion:

1. To add conditional restrictions to the left table, add them in the where condition and do not put them after on.
2. Adding conditional restrictions to the right table needs to be placed after on. Adding them in the where condition will cause problems with the number of data rows.

This is the end of this article about analyzing the problem of using left join to add where conditions in MySQL. For more relevant content about adding where to MySQL left join, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Analysis of the difference between the usage of left join setting conditions in on and where in mysql
  • Introduction to the difference between on and where conditions in MySQL left join operation
  • Analyzing the difference between using on and where filtering in MySQL left (right) join

<<:  JavaScript to implement search data display

>>:  10 Underused or Misunderstood HTML Tags

Recommend

Docker memory monitoring and stress testing methods

The Docker container that has been running shows ...

Detailed explanation of application scenarios of filters in Vue

filter is generally used to filter certain values...

New settings for text and fonts in CSS3

Text Shadow text-shadow: horizontal offset vertic...

A brief discussion on the mysql execution process and sequence

Table of contents 1:mysql execution process 1.1: ...

React hooks introductory tutorial

State Hooks Examples: import { useState } from &#...

Docker image import and export code examples

Import and export of Docker images This article i...

Specific use of Docker anonymous mount and named mount

Table of contents Data volume Anonymous and named...

How to use display:olck/none to create a menu bar

The effect of completing a menu bar through displ...

Solution to CSS flex-basis text overflow problem

The insignificant flex-basis has caused a lot of ...

Record the steps of using mqtt server to realize instant communication in vue

MQTT Protocol MQTT (Message Queuing Telemetry Tra...

Solve the problem that Docker cannot ping the host machine under Mac

Solution Abandon the Linux virtual machine that c...

CSS3 achieves cool 3D rotation perspective effect

CSS3 achieves cool 3D rotation perspective 3D ani...

Implementation of proxy_pass in nginx reverse proxy

The format is simple: proxy_pass URL; The URL inc...

A brief discussion on Flex layout and scaling calculation

1. Introduction to Flex Layout Flex is the abbrev...

Mysql uses insert to insert multiple records to add data in batches

If you want to insert 5 records into table1, the ...