sql statement left the difference between analysis and inner join join in on and where the
About SQL SERVER table join query INNER JOIN, LEFT JOIN and RIGHT JOIN, and often used condition ON WHERE query, use the time before the seat of your pants sometimes, not always clear, personally tested the next day, understanding to some of the content in this share.
To test, first we have to create three tables, the database according to their own circumstances
Create a table TestJoinOnOrWhere_A, TestJoinOnOrWhere_B, TestJoinOnOrWhere_C
/****** Object: Table [dbo].[TestJoinOnOrWhere_A] Script Date: 2015/4/3 14:34:41 ******/ CREATE TABLE [dbo].[TestJoinOnOrWhere_A]( [id] [int] NULL, [value] [int] NULL ) ON [PRIMARY] GO /****** Object: Table [dbo].[TestJoinOnOrWhere_B] Script Date: 2015/4/3 14:34:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[TestJoinOnOrWhere_B]( [id] [int] NULL, [value] [int] NULL ) ON [PRIMARY] GO /****** Object: Table [dbo].[TestJoinOnOrWhere_C] Script Date: 2015/4/3 14:34:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[TestJoinOnOrWhere_C]( [id] [int] NULL, [value] [int] NULL ) ON [PRIMARY]
Created the table and then we add a few data
INSERT [dbo].[TestJoinOnOrWhere_A] ([id], [value]) VALUES (1, 1) INSERT [dbo].[TestJoinOnOrWhere_A] ([id], [value]) VALUES (2, 1) INSERT [dbo].[TestJoinOnOrWhere_A] ([id], [value]) VALUES (3, 2) INSERT [dbo].[TestJoinOnOrWhere_B] ([id], [value]) VALUES (1, 1) INSERT [dbo].[TestJoinOnOrWhere_B] ([id], [value]) VALUES (2, 3) INSERT [dbo].[TestJoinOnOrWhere_B] ([id], [value]) VALUES (3, 4) INSERT [dbo].[TestJoinOnOrWhere_C] ([id], [value]) VALUES (1, 1) INSERT [dbo].[TestJoinOnOrWhere_C] ([id], [value]) VALUES (2, 2) INSERT [dbo].[TestJoinOnOrWhere_C] ([id], [value]) VALUES (3, 3)
Now we start testing
语句1:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 语句2:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id
Result 1:
id value id value
-------------------------------
1 1 1 1
2 1 2 3
3:02 NULL NULL
Results
id value id value
-------------------------------
1 1 1 1
2 1 2 3
3 2 3 4
Check online to, some people say a.value = 1 is not in force, it is not true, it is already in force, but in the left join query, the data table on the left are not affected, only the data table on the right will be based on a. value = 1 condition remove the left table (a table) value row 1, you can see by the results of the above two statements, then what we will use the right filter criteria table it? Consider the following statement
语句3:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND b.value = 1
Results:
id value id value
-------------------------------
1:01 1:01
2:01 NULL NULL
3:02 NULL NULL
The results seen above, only the influence of the right table data
语句4:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.value = 1
Result 4:
id value id value
-------------------------------
1:01 1:01
1:01 2:03
1:01 3:04
2:01 1:01
2 1 2 3
2:01 3:04
3:02 NULL NULL
From the above results seen in the statement, but also affects only the right data table (a table value corresponding to remove all of the data in Table 1 b)
It will only affect the right table at the back of the left join query ON condition, the opposite effect is the right join query the data table on the left
If WHERE it? We look at the following statement
语句5:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id where a.value = 1 语句6:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id where b.value = 1
5 results:
id value id value
-------------------------------
1 1 1 1
2 1 2 3
Results 6:
id value id value
-------------------------------
1 1 1 1
From the results, the results of this table is the total impact, results equivalent join query queries an ON condition, and then taken through the back of the overall screening WHERE condition
What impact will the ON INNER JOIN conditions? Look at the results of the following statements
语句7: SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 语句8: SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND b.value = 1 语句9: SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id WHERE a.value = 1 语句10:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id WHERE b.value = 1
7/9 results:
id value id value
-------------------------------
1 1 1 1
2 1 2 3
8/10 results:
id value id value
-------------------------------
1 1 1 1
WHERE out by the above query result is the same, and ON, which can be seen, the influence of the ON INNER JOIN and WHERE condition is a condition effect, affect the overall results of the query.
Here we look at three tables for LEFT JOIN query results for the impact of WHERE and ON
语句11:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id
语句12:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND b.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id
语句13:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id AND b.value = 1
语句14:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id AND c.value = 2
Results 11:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1:01 1:01 1:01
2:01 2:03 2:02
3:02 NULL NULL NULL NULL
Results 12:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1:01 1:01 1:01
2:01 NULL NULL NULL NULL
3:02 NULL NULL NULL NULL
Results 13:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1:01 1:01 1:01
2:01 2:03 NULL NULL
3:02 NULL NULL NULL NULL
Results 14:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1 1 1 1 NULL NULL
2:01 2:03 2:02
3:02 NULL NULL NULL NULL
By the above three tables of data query results, it can be seen, LEFT JOIN query for a separate table at right ON condition is always only affect the condition table (e.g., a.value = 1 b will affect the value table associated with a field value table row 1, and will not limit the data tables show only a row value = 1), RIGHT JOIN opposite affect the results
When the ON LEFT JOIN conditions that affect the second third table to the right, it will not affect the leftmost table, so for a, b, c, three tables, the table data is not A Effect ON conditions, b or c only affect join query data
And WHERE is equivalent to the data before the query's WHERE condition in front of a table, and then filter the data by WHERE condition, so the impact is overall.
Created: 2015-04-03
This article comes from wl131710, please indicate the source: http://www.cnblogs.com/wanglu/p/4390612.html
About SQL SERVER table join query INNER JOIN, LEFT JOIN and RIGHT JOIN, and often used condition ON WHERE query, use the time before the seat of your pants sometimes, not always clear, personally tested the next day, understanding to some of the content in this share.
To test, first we have to create three tables, the database according to their own circumstances
Create a table TestJoinOnOrWhere_A, TestJoinOnOrWhere_B, TestJoinOnOrWhere_C
/****** Object: Table [dbo].[TestJoinOnOrWhere_A] Script Date: 2015/4/3 14:34:41 ******/ CREATE TABLE [dbo].[TestJoinOnOrWhere_A]( [id] [int] NULL, [value] [int] NULL ) ON [PRIMARY] GO /****** Object: Table [dbo].[TestJoinOnOrWhere_B] Script Date: 2015/4/3 14:34:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[TestJoinOnOrWhere_B]( [id] [int] NULL, [value] [int] NULL ) ON [PRIMARY] GO /****** Object: Table [dbo].[TestJoinOnOrWhere_C] Script Date: 2015/4/3 14:34:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[TestJoinOnOrWhere_C]( [id] [int] NULL, [value] [int] NULL ) ON [PRIMARY]
Created the table and then we add a few data
INSERT [dbo].[TestJoinOnOrWhere_A] ([id], [value]) VALUES (1, 1) INSERT [dbo].[TestJoinOnOrWhere_A] ([id], [value]) VALUES (2, 1) INSERT [dbo].[TestJoinOnOrWhere_A] ([id], [value]) VALUES (3, 2) INSERT [dbo].[TestJoinOnOrWhere_B] ([id], [value]) VALUES (1, 1) INSERT [dbo].[TestJoinOnOrWhere_B] ([id], [value]) VALUES (2, 3) INSERT [dbo].[TestJoinOnOrWhere_B] ([id], [value]) VALUES (3, 4) INSERT [dbo].[TestJoinOnOrWhere_C] ([id], [value]) VALUES (1, 1) INSERT [dbo].[TestJoinOnOrWhere_C] ([id], [value]) VALUES (2, 2) INSERT [dbo].[TestJoinOnOrWhere_C] ([id], [value]) VALUES (3, 3)
Now we start testing
语句1:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 语句2:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id
Result 1:
id value id value
-------------------------------
1 1 1 1
2 1 2 3
3:02 NULL NULL
Results
id value id value
-------------------------------
1 1 1 1
2 1 2 3
3 2 3 4
Check online to, some people say a.value = 1 is not in force, it is not true, it is already in force, but in the left join query, the data table on the left are not affected, only the data table on the right will be based on a. value = 1 condition remove the left table (a table) value row 1, you can see by the results of the above two statements, then what we will use the right filter criteria table it? Consider the following statement
语句3:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND b.value = 1
Results:
id value id value
-------------------------------
1:01 1:01
2:01 NULL NULL
3:02 NULL NULL
The results seen above, only the influence of the right table data
语句4:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.value = 1
Result 4:
id value id value
-------------------------------
1:01 1:01
1:01 2:03
1:01 3:04
2:01 1:01
2 1 2 3
2:01 3:04
3:02 NULL NULL
From the above results seen in the statement, but also affects only the right data table (a table value corresponding to remove all of the data in Table 1 b)
It will only affect the right table at the back of the left join query ON condition, the opposite effect is the right join query the data table on the left
If WHERE it? We look at the following statement
语句5:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id where a.value = 1 语句6:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id where b.value = 1
5 results:
id value id value
-------------------------------
1 1 1 1
2 1 2 3
Results 6:
id value id value
-------------------------------
1 1 1 1
From the results, the results of this table is the total impact, results equivalent join query queries an ON condition, and then taken through the back of the overall screening WHERE condition
What impact will the ON INNER JOIN conditions? Look at the results of the following statements
语句7: SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 语句8: SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND b.value = 1 语句9: SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id WHERE a.value = 1 语句10:SELECT * FROM dbo.TestJoinOnOrWhere_A AS a INNER JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id WHERE b.value = 1
7/9 results:
id value id value
-------------------------------
1 1 1 1
2 1 2 3
8/10 results:
id value id value
-------------------------------
1 1 1 1
WHERE out by the above query result is the same, and ON, which can be seen, the influence of the ON INNER JOIN and WHERE condition is a condition effect, affect the overall results of the query.
Here we look at three tables for LEFT JOIN query results for the impact of WHERE and ON
语句11:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id
语句12:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND b.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id
语句13:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id AND b.value = 1
语句14:SELECT a.id AS a_id,a.value AS a_value,b.id AS b_id,b.value AS b_value,c.id AS c_id,c.value AS c_value FROM dbo.TestJoinOnOrWhere_A AS a LEFT JOIN dbo.TestJoinOnOrWhere_B AS b ON a.id = b.id AND a.value = 1 LEFT JOIN dbo.TestJoinOnOrWhere_C AS c ON b.id = c.id AND c.value = 2
Results 11:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1:01 1:01 1:01
2:01 2:03 2:02
3:02 NULL NULL NULL NULL
Results 12:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1:01 1:01 1:01
2:01 NULL NULL NULL NULL
3:02 NULL NULL NULL NULL
Results 13:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1:01 1:01 1:01
2:01 2:03 NULL NULL
3:02 NULL NULL NULL NULL
Results 14:
a_id a_value b_id b_value c_id c_value
-------------------------------------------------- ---
1 1 1 1 NULL NULL
2:01 2:03 2:02
3:02 NULL NULL NULL NULL
By the above three tables of data query results, it can be seen, LEFT JOIN query for a separate table at right ON condition is always only affect the condition table (e.g., a.value = 1 b will affect the value table associated with a field value table row 1, and will not limit the data tables show only a row value = 1), RIGHT JOIN opposite affect the results
When the ON LEFT JOIN conditions that affect the second third table to the right, it will not affect the leftmost table, so for a, b, c, three tables, the table data is not A Effect ON conditions, b or c only affect join query data
And WHERE is equivalent to the data before the query's WHERE condition in front of a table, and then filter the data by WHERE condition, so the impact is overall.
Created: 2015-04-03
This article comes from wl131710, please indicate the source: http://www.cnblogs.com/wanglu/p/4390612.html