1. 程式人生 > >SQL關聯查詢 直接join 和子查詢的區別

SQL關聯查詢 直接join 和子查詢的區別

proc 認證 ado con pan exp type rip 圖片

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 和子查詢的區別