关于编写 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/