The difference between on and where in a left join, inner join in an SQL statement
Table A (ID, type):
ID Type
----------------------------------
1 1
2 1
3 2
Table B (ID, Class):
ID class
---------------------------------
1 1
2 2
SQL statement 1:select a.*, b.* from a LEFT join B on a.id = b.id and a.type = 1;
SQL statement 2:select a.*, b.* from a LEFT join B on a.id = b.id where a.type = 1;
SQL statement 3:select a.*, b.* from a LEFT join B on a.id = b.id and b.class = 1;
The result of SQL statement 1 is:
a.ID A.type b.id B.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
The result of SQL Statement 2 is:
a.ID A.type b.id B.class
----------------------------------------
1 1 1 1
2 1 2 2
The result of SQL statement 3 is:
a.ID A.type b.id B.class
----------------------------------------
1 1 1 1
2 1
3 2
Visible from SQL statement 1, all records of the left table in the right join will be fully queried, and the conditions on the back of it won't work, unless you add a where to filter it, and this is SQL Statement 2, which is visible by SQL statement 3, in the condition after on, the constraints on the table that are in the list will work.
**********************************************************************************
SQL statement 4:select a.*, b.* from a inner join B on a.id = b.id and a.type = 1;
SQL statement 5:select a.*, b.* from a inner join B on a.id = b.id where a.type = 1;
SQL statements 6:select a.*, b.* from A, b where a.id = b.id and a.type = 1;
SQL statements 7:select a.*, b.* from A, b where A.type = 1 and a.id = b.id;
The execution results of these four statements are as follows:
a.ID A.type b.id B.class
----------------------------------------
1 1 1 1
2 1 2 2
Thus, the restrictions on the back of inner join will all work, as is the case with where execution results are the same. In addition, the where statement and the inner join do get the same result, but the efficiency is different (I haven't tested it, but I believe it).
But if SQL statement 6 is less efficient than SQL statement 7, I don't have enough data to test, but I do believe so.
The difference between on and where in a left join, inner join in an SQL statement