Article Directory
Scenes
In actual work, it join
is often used. Among them, on
and where
are often used together. But the results will join
show 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.
-
If we want to get the data in the table
userone
with theusertwo
same name as the table whose gender is male, howSQL
can 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,
SQL
the results of the above two lines are consistent. The results are as follows:
-
If we want to get the table
userone
data, and highlight the data with theusertwo
same name as the table, but the gender is male, howSQL
can we write it? ? ? Someone might write the followingSQL
, 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,
SQL
the results of the above two lines are inconsistent, andSQL1
the result obtained is:
SQL2
The result obtained is:
for the given title, the result we want isSQL1
because we want to get all the data, and we need to indicate the data with theusertwo
same name as the table, but the gender is male. This is reflectedjoin
inon
thewhere
difference. If the reader is more careful, you will find that the latter twoSQL
use left joinsleft 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. Theon
latter 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. Thewhere
keyword filters the filtered result set and is effective for the whole. So,SQL1
andSQL2
differences in point of view, the results are very different.
to sum up
join
In on
it is the Cartesian product of two tables filtering, and where
is associated with the final results of the data out of the filter. If it is a left-right connection, on
the conditions inside will ignore the unsatisfied data in the main table and do not filter; instead, where
it 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!