SQL left join vs multiple tables on FROM line?

前端 未结 11 2385
予麋鹿
予麋鹿 2020-11-22 05:08

Most SQL dialects accept both the following queries:

SELECT a.foo, b.foo
FROM a, b
WHERE a.x = b.x

SELECT a.foo, b.foo
FROM a
LEFT JOIN b ON a.x = b.x


        
相关标签:
11条回答
  • 2020-11-22 05:41

    When you need an outer join the second syntax is not always required:

    Oracle:

    SELECT a.foo, b.foo
      FROM a, b
     WHERE a.x = b.x(+)
    

    MSSQLServer (although it's been deprecated in 2000 version)/Sybase:

    SELECT a.foo, b.foo
      FROM a, b
     WHERE a.x *= b.x
    

    But returning to your question. I don't know the answer, but it is probably related to the fact that a join is more natural (syntactically, at least) than adding an expression to a where clause when you are doing exactly that: joining.

    0 讨论(0)
  • 2020-11-22 05:43

    Well the first and second queries may yield different results because a LEFT JOIN includes all records from the first table, even if there are no corresponding records in the right table.

    0 讨论(0)
  • 2020-11-22 05:46

    The JOIN syntax keeps conditions near the table they apply to. This is especially useful when you join a large amount of tables.

    By the way, you can do an outer join with the first syntax too:

    WHERE a.x = b.x(+)
    

    Or

    WHERE a.x *= b.x
    

    Or

    WHERE a.x = b.x or a.x not in (select x from b)
    
    0 讨论(0)
  • 2020-11-22 05:48

    Basically, when your FROM clause lists tables like so:

    SELECT * FROM
      tableA, tableB, tableC
    

    the result is a cross product of all the rows in tables A, B, C. Then you apply the restriction WHERE tableA.id = tableB.a_id which will throw away a huge number of rows, then further ... AND tableB.id = tableC.b_id and you should then get only those rows you are really interested in.

    DBMSs know how to optimise this SQL so that the performance difference to writing this using JOINs is negligible (if any). Using the JOIN notation makes the SQL statement more readable (IMHO, not using joins turns the statement into a mess). Using the cross product, you need to provide join criteria in the WHERE clause, and that's the problem with the notation. You are crowding your WHERE clause with stuff like

        tableA.id = tableB.a_id 
    AND tableB.id = tableC.b_id 
    

    which is only used to restrict the cross product. WHERE clause should only contain RESTRICTIONS to the resultset. If you mix table join criteria with resultset restrictions, you (and others) will find your query harder to read. You should definitely use JOINs and keep the FROM clause a FROM clause, and the WHERE clause a WHERE clause.

    0 讨论(0)
  • 2020-11-22 05:51

    I think there are some good reasons on this page to adopt the second method -using explicit JOINs. The clincher though is that when the JOIN criteria are removed from the WHERE clause it becomes much easier to see the remaining selection criteria in the WHERE clause.

    In really complex SELECT statements it becomes much easier for a reader to understand what is going on.

    0 讨论(0)
  • 2020-11-22 05:52

    I hear a lot of people complain the first one is too difficult to understand and that it is unclear. I don't see a problem with it, but after having that discussion, I use the second one even on INNER JOINS for clarity.

    0 讨论(0)
提交回复
热议问题