SQL关联查询 直接join 和子查询的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL关联查询 直接join 和子查询的区别相关的知识,希望对你有一定的参考价值。

SQL语句执行的时候是有一定顺序的。

1.from
先选择一个表,构成一个结果集。

2.where
对结果集进行筛选,筛选出需要的信息形成新的结果集。

3.group by
对新的结果集分组。

4.having
筛选出想要的分组。

5.select
选择列。

6.order by
当所有的条件都弄完了。最后排序。

explain select  users.`mobile_num`, concat(users.`lastName` ,users.`firstName`) as userName, users.`company`,
  (case `users`.`idPhotoCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `idPhotoCheckStatus`,
  (case `users`.`driverLicenseCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `driverLicenseCheckStatus`,
  (case `users`.`companyCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `companyCheckStatus`,
  (case `users`.`unionCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `unionCheckStatus`,
 ptrip_num, dtrip_num
from users 
 left  join 
 (select count(passenger_trip.id)  as ptrip_num, passenger_trip.`userId` from  passenger_trip where  passenger_trip.status != ‘cancel‘ group by passenger_trip.`userId` ) as ptrip
 on ptrip.userId = users.id
 left join 
 (select count(driver_trip.id)  as dtrip_num, driver_trip.`userId` from  driver_trip where  driver_trip.status != ‘cancel‘ group by driver_trip.`userId` ) as dtrip
 on dtrip.userId = users.id
where company != ‘上海狮博实业投资有限公司‘ and company != ‘88共享‘

技术分享图片

explain select  users.`mobile_num`, concat(users.`lastName` ,users.`firstName`) as userName, users.`company`,
  (case `users`.`idPhotoCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `idPhotoCheckStatus`,
  (case `users`.`driverLicenseCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `driverLicenseCheckStatus`,
  (case `users`.`companyCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `companyCheckStatus`,
  (case `users`.`unionCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `unionCheckStatus`,
  (select count(passenger_trip.id) from  passenger_trip where  passenger_trip.userId = users.id  and passenger_trip.status != ‘cancel‘) as ptrip_num,
  (select count(driver_trip.id) from  driver_trip where  driver_trip.userId = users.id  and driver_trip.status != ‘cancel‘) as dtrip_num
from users
where company != ‘上海狮博实业投资有限公司‘ and company != ‘88共享‘

技术分享图片

explain select  users.`mobile_num`, concat(users.`lastName` ,users.`firstName`) as userName, users.`company`,
  (case `users`.`idPhotoCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `idPhotoCheckStatus`,
  (case `users`.`driverLicenseCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `driverLicenseCheckStatus`,
  (case `users`.`companyCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `companyCheckStatus`,
  (case `users`.`unionCheckStatus` when ‘2‘ then ‘已认证‘ when ‘3‘ then ‘已驳回‘ else ‘待认证‘ end) as `unionCheckStatus`,
  count(passenger_trip.id) as ptrip_num
from users
left join passenger_trip on passenger_trip.userId = users.id  and passenger_trip.status != ‘cancel‘
left join driver_trip on driver_trip.`userId`=users.`id` and driver_trip.`status` != ‘cancel‘
where company != ‘上海狮博实业投资有限公司‘ and company != ‘88共享‘

技术分享图片

以上是关于SQL关联查询 直接join 和子查询的区别的主要内容,如果未能解决你的问题,请参考以下文章

关于SQL 查询效率问题 left join 改成 inner join union

子查询与关联查询区别

mysql 关联查询是不是很耗性能

SQL-连接查询:left join,right join,inner join,full join之间的区别

SQL JOIN 数据库表关联关系

mysql on和where区别