谁还没个明天 2009-06-19 16:16 采纳率: 50%
浏览 196
已采纳

内部连接 ON vs WHERE 子句

For simplicity, assume all relevant fields are NOT NULL.

You can do:

SELECT
    table1.this, table2.that, table2.somethingelse
FROM
    table1, table2
WHERE
    table1.foreignkey = table2.primarykey
    AND (some other conditions)

Or else:

SELECT
    table1.this, table2.that, table2.somethingelse
FROM
    table1 INNER JOIN table2
    ON table1.foreignkey = table2.primarykey
WHERE
    (some other conditions)

Do these two work on the same way in MySQL?

转载于:https://stackoverflow.com/questions/1018822/inner-join-on-vs-where-clause

  • 写回答

10条回答 默认 最新

  • from.. 2009-06-19 16:17
    关注

    INNER JOIN is ANSI syntax which you should use.

    It is generally considered more readable, especially when you join lots of tables.

    It can also be easily replaced with an OUTER JOIN whenever a need arises.

    The WHERE syntax is more relational model oriented.

    A result of two tables JOINed is a cartesian product of the tables to which a filter is applied which selects only those rows with joining columns matching.

    It's easier to see this with the WHERE syntax.

    As for your example, in MySQL (and in SQL generally) these two queries are synonyms.

    Also note that MySQL also has a STRAIGHT_JOIN clause.

    Using this clause, you can control the JOIN order: which table is scanned in the outer loop and which one is in the inner loop.

    You cannot control this in MySQL using WHERE syntax.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM