码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-11-09 21:07:34      阅读:497      评论:0      收藏:0      [点我收藏+]

标签:http   exp   else   一个   png   count   log   pre   有限公司   

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 和子查询的区别

标签:http   exp   else   一个   png   count   log   pre   有限公司   

原文地址:http://blog.51cto.com/10705830/2315247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!