【问题标题】:What is the difference between these two sql queries with and without join? [duplicate]这两个带和不带连接的 sql 查询有什么区别? [复制]
【发布时间】:2015-09-04 11:41:47
【问题描述】:

查询 1。

select friends.name,social.facebook   
from friends inner join social on friends.id=social.fid where facebook="Yes";

输出。

+-----------------+
| name | facebook |
+-----------------+
| ABC  |   Yes    |
| BCD  |   Yes    |
| CDE  |   Yes    |
+-----------------+

查询 2。

select f.name,s.facebook from friends as f,social as s   
where f.id=s.fid && s.facebook="Yes";

输出。

+-----------------+
| name | facebook |
+-----------------+
| ABC  |   Yes    |
| BCD  |   Yes    |
| CDE  |   Yes    |
+-----------------+

哪种方式更可靠更快捷?

【问题讨论】:

    标签: mysql


    【解决方案1】:

    在结果集方面没有区别,但您的第一个查询如下是使用 ANSI 样式的显式连接语法

    select friends.name,social.facebook 
    from friends inner join social on friends.id=social.fid 
    where social.facebook="Yes"; //also qualify the column with alias/table name
    

    而下面的第二个查询使用旧式隐式连接语法。

    select f.name,s.facebook from friends as f,social as s 
    where f.id=s.fid && s.facebook="Yes"
    

    首选JOIN 的第一种样式,因为它更具可读性并且更清楚您的意图。

    【讨论】:

      【解决方案2】:

      我会说 Query1 符合 ANSI 标准并且是可取的。如果省略 WHERE 条件从而提供不需要的数据,则 Query2 变为交叉连接。在 Query1 中,如果未指定 JOINed 列,则会出现更可靠的错误

      【讨论】:

      • 如果您遗漏了ON 子句,MySQL 不会给出错误消息。它只是把它当作CROSS JOIN
      猜你喜欢
      • 2014-02-08
      • 2012-08-05
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 2010-12-19
      相关资源
      最近更新 更多