开发者

What is the difference between `FROM _ , _` and `FROM _ INNER JOIN _ ON _`

开发者 https://www.devze.com 2023-03-23 15:52 出处:网络
What is the Difference between the two? SELECT [EmployeeList].[Emp_ID], [EmployeeLevel].[LevelPosition]

What is the Difference between the two?

SELECT [EmployeeList].[Emp_ID], [EmployeeLevel].[LevelPosition]
FROM [EmployeeList], [E开发者_开发问答mployeeLevel]

SELECT [EmployeeList].[Emp_ID], [EmployeeLevel].[LevelPosition]
FROM EmployeeList INNER JOIN EmployeeLevel ON 
[EmployeeList].[LevelID] = [EmployeeLevel].[LevelID]

regardless of the field names.


The first one is not correlated in any way and will return a cross join / cartesian join with every permutation of rows from the 2 tables. You would need to add a WHERE clause

SELECT [EmployeeList].[Emp_ID], [EmployeeLevel].[LevelPosition]
FROM [EmployeeList], [EmployeeLevel]
WHERE [EmployeeList].[LevelID] = [EmployeeLevel].[LevelID]

Then they would be semantically the same but the above is the old style ANSI syntax and is largely discouraged by everyone except Joe Celko due to being less clear and the possibility of causing inadvertent cartesian Joins (as well as being more work to change if you want to convert to an outer join).


Implicit Cross Join vs. Inner Join

http://explainextended.com/2009/07/16/inner-join-vs-cross-apply/

http://en.wikipedia.org/wiki/Join_(SQL)


First one you're selecting a list of employees ids and a list of level positions. (with no necessarily relationship between them).

On the second one you're doing a join. You're relating the list of employees (given its level) with the employees with the same level (see the ON condition).


The first is a Cartesian Product join which does not care about matching rows between the two tables. It creates a list of every combination of row from table1 combined with every row from table2. Unless there is a specific case requiring it, this kind of query produces way too much data of little meaning.

The second matches rows with equivalent IDs.


First one shows all data from EmployeeList and EmployeeLevel table... In second query, there are a rlation between EmployeeList and EmployeeLevel by LevelID and INNER JOIN means must match between EmployeeList.LevelID and EmployeeLevel.LevelID... So second query shows data if match both LevelID....

0

精彩评论

暂无评论...
验证码 换一张
取 消