CROSS JOIN vs INNER JOIN in SQL

前端 未结 12 926
醉酒成梦
醉酒成梦 2020-11-22 03:16

What is the difference between CROSS JOIN and INNER JOIN?

CROSS JOIN:

SELECT 
    Movies.CustomerID, Movie         


        
12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:30

    It depends on the output you expect.

    A cross join matches all rows in one table to all rows in another table. An inner join matches on a field or fields. If you have one table with 10 rows and another with 10 rows then the two joins will behave differently.

    The cross join will have 100 rows returned and they won't be related, just what is called a Cartesian product. The inner join will match records to each other. Assuming one has a primary key and that is a foreign key in the other you would get 10 rows returned.

    A cross join has limited general utility, but exists for completeness and describes the result of joining tables with no relations added to the query. You might use a cross join to make lists of combinations of words or something similar. An inner join on the other hand is the most common join.

提交回复
热议问题