CROSS JOIN vs INNER JOIN in SQL

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

What is the difference between CROSS JOIN and INNER JOIN?

CROSS JOIN:

SELECT 
    Movies.CustomerID, Movie         


        
12条回答
  •  离开以前
    2020-11-22 03:39

    Cross join and inner join are the same with the only difference that in inner join we booleanly filter some of the outcomes of the cartesian product

    table1
    x--------------------------------------x
    |  fieldA  |    fieldB   |    fieldC   | 
    x----------|-------------|-------------x            
    |    A     |      B      |    option1  |
    |    A     |      B1     |    option2  |
    x--------------------------------------x
    
    table2
    x--------------------------------------x
    |  fieldA  |    fieldB   |    fieldC   | 
    x----------|-------------|-------------x            
    |    A     |      B      |    optionB1 |
    |    A1    |      B1     |    optionB2 |
    x--------------------------------------x
    
     cross join
      A,B,option1,A,B,optionB1
      A,B,option1,A1,B1,optionB2
      A,B1,option2,A,B,optionB1
      A,B1,option2,A1,B1,optionB2
    
     inner join on field1 (only with the value is the same in both tables)
      A,B,option1,A,B,optionB1
      A,B1,option2,A,B,optionB1
    
     inner join on field1
      A,B,option1,A,B,optionB1
    

    It is on design of our data where we decide that there is only one case of the field we are using for the join. Join only cross join both tables and get only the lines accomplishing special boolean expression.

    Note that if the fields we are doing our Joins on would be null in both tables we would pass the filter. It is up to us or the database manufacturer to add extra rules to avoid or permit nulls. Adhering to the basics it is just a cross join followed by a filter.

提交回复
热议问题