Database learning series: the difference between on and where in join

Scenes

In actual work, it joinis often used. Among them, onand whereare often used together. But the results will joinshow different results depending on the use . This blog is mainly about the difference.

surroundings

software version
mysql 5.7.27

text

1. Data preparation

1. Create a table statement

CREATE TABLE userone  ( 
	username	varchar(255) NULL,
	sex     	smallint(255)  COMMENT '1.男;2.女'  NULL,
	score   	int(255) NULL 
);
CREATE TABLE usertwo  ( 
	username	varchar(255) NULL,
	sex     	smallint(255)  COMMENT '1.男;2.女'  NULL,
	score   	int(255) NULL 
);

2. Data Insertion

INSERT INTO userone(username, sex, score)
  VALUES('name0', 1, 123);
INSERT INTO userone(username, sex, score)
  VALUES('name1', 2, 12);
INSERT INTO userone(username, sex, score)
  VALUES('name2', 1, 2);
INSERT INTO userone(username, sex, score)
  VALUES('name3', 2, 3);
INSERT INTO userone(username, sex, score)
  VALUES('name0', 2, 333);

INSERT INTO usertwo(username, sex, score)
  VALUES('name0', 1, 31);
INSERT INTO usertwo(username, sex, score)
  VALUES('name1', 2,22);
INSERT INTO usertwo(username, sex, score)
  VALUES('name4', 1, 42);
INSERT INTO usertwo(username, sex, score)
  VALUES('name5', 2, 53);

Two, test

Here we analyze it with a case.

  1. If we want to get the data in the table useronewith the usertwosame name as the table whose gender is male, how SQLcan we write it? ? ?

    SELECT a.username,a.sex,a.score,b.username,b.sex,b.score 
    FROM userone a join usertwo b on a.username=b.username
    where a.sex=1
    
    SELECT a.username,a.sex,a.score,b.username,b.sex,b.score 
    FROM userone a join usertwo b on a.username=b.username and a.sex=1
    

    In terms of results, SQLthe results of the above two lines are consistent. The results are as follows:
    Insert picture description here

  2. If we want to get the table useronedata, and highlight the data with the usertwosame name as the table, but the gender is male, how SQLcan we write it? ? ? Someone might write the following SQL, as follows

    -- SQL1
    SELECT a.username,a.sex,a.score,case when b.username is not null then '存在' else '不存在' end as '标记'
    FROM userone a left join usertwo b on a.username=b.username and a.sex=1
    -- SQL2
    SELECT a.username,a.sex,a.score,case when b.username is not null then '存在' else '不存在' end as '标记'
    FROM userone a left join usertwo b on a.username=b.username
    where a.sex=1
    

    As far as the result is concerned, SQLthe results of the above two lines are inconsistent, and SQL1the result obtained is:
    Insert picture description here

    SQL2The result obtained is:
    Insert picture description here
    for the given title, the result we want is SQL1because we want to get all the data, and we need to indicate the data with the usertwosame name as the table, but the gender is male. This is reflected joinin onthe wheredifference. If the reader is more careful, you will find that the latter two SQLuse left joins left join. The left connection means that we want to use the left table as the benchmark, and even if the conditions are not met, we must put it into the result set as the association. The onlatter conditions are not valid for the main table, because even if the conditions are not met, according to the requirements of the left and right connections, the data that does not meet the conditions in the main table must be put into the main table. The wherekeyword filters the filtered result set and is effective for the whole. So, SQL1and SQL2differences in point of view, the results are very different.

to sum up

joinIn onit is the Cartesian product of two tables filtering, and whereis associated with the final results of the data out of the filter. If it is a left-right connection, onthe conditions inside will ignore the unsatisfied data in the main table and do not filter; instead, whereit will filter the associated result set without considering the left-right association.

Ask for praise

If my article is helpful to everyone, you can click like or favorite at the bottom of the article;
if there is a good discussion, you can leave a message;
if you want to continue to view my future articles, you can click Follow
You can scan the following QR code to follow me 'S public account: Fengye Zhixuege, check out my latest share!
Insert picture description here
Bye bye

Guess you like

Origin blog.csdn.net/u013084266/article/details/113702591