step1:使用driver_id相等的条件,将online_time_tb左连接driver_tb;
step2:使用city_id相等的条件,左连接city_tb;
step3:限定条件为北京、8月和9月的新司机;
step4:按月份分组,求在线总时长。
SELECT DATE_FORMAT(a.dt,'%Y-%m') AS month, SUM(online_len) as 'online_len'
FROM online_time_tb a
LEFT JOIN driver_tb b using(driver_id)
LEFT JOIN city_tb c using(city_id)
WHERE c.city_name='北京' and ((DATE_FORMAT(b.finish_dt, '%Y-%m')='2021-08'
AND DATE_FORMAT(a.dt, '%Y-%m')='2021-08')
OR (DATE_FORMAT(b.finish_dt, '%Y-%m')='2021-09'
AND DATE_FORMAT(a.dt, '%Y-%m')='2021-09'))
GROUP BY DATE_FORMAT(a.dt,'%Y-%m');