mysql - 这两种类型的查询编写有什么区别?

标签 mysql sql

关于编写 SQL 查询,我可以通过两种方式获取我需要的东西:

类型 1:

select 
    cl.id 
from 
    datahub_clients             cl,
    datahub_client_accounts     cl_ac,
    datahub_accounts            ac    
where
    cl.id=cl_ac.client_id
    and
    ac.id=cl_ac.account_id
    and
    ac.account_no_dataphile="7H0010A1"
;

类型 2:

select 
    cl.id 
from 
    datahub_clients             cl
    join
    datahub_client_accounts     cl_ac
    on
    cl.id=cl_ac.client_id
    join
    datahub_accounts            ac    
    on
    ac.id=cl_ac.account_id
where
    ac.account_no_dataphile="7H0010A1"
;

以下是我心中的几个疑问: 1)SQL编译器对它们的解释有什么区别吗? 2)是否应该对任何一个人有偏好? (为什么?) 3)从可扩展性的角度来看,哪一个更好?

最佳答案

Type 2:

select 
    cl.id 
from 
    datahub_clients             cl
    join
    datahub_client_accounts     cl_ac
    on
    cl.id=cl_ac.client_id
    join
    datahub_accounts            ac    
    on
    ac.id=cl_ac.account_id
where
    ac.account_no_dataphile="7H0010A1"

更好,因为用逗号分隔的表名在所有这些表之间创建交叉联接(这在性能方面代价高昂),因此应该避免

Type-1 是旧的连接方法,强烈建议不要使用

关于mysql - 这两种类型的查询编写有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52970689/

相关文章:

mysql - 来自教程的右外连接

mysql - 从订阅列表中如何获取仅订阅英语 channel 的用户

sql - 需要帮助构建 SQL 查询(简单连接)

mysql - 检索每组中的最后一条记录 - MySQL

mysqld.exe - Laragon 启动时出现应用程序错误

mysql - 通过查找最大值来填充列

使用 group by 聚合计数 { >100 万用户} 的 Mysql 查询性能变慢

sql - 如何检查SQL Server 2008中用户定义表类型是否存在?

mysql - 数据库创建失败,结果未定义

mysql - 限制组中的每个组