Mysql uses union all to count the total number of combinations of multiple tables, and count the number of tables separately

select count(distinct(vehicleId)) as ‘总数’,
count(distinct(case when flag = ‘t2’ then vehicleId end)) as ‘表2数量’
from
(
SELECT ‘t1’ as flag, cf.vehicle_id as vehicleId FROM ota_campaign_feedback cf WHERE cf.campaign_id = 1021612 GROUP BY cf.vehicle_id
union all
SELECT ‘t2’ as flag, c.vehicle_id as vehicleId FROM ota_vehicle_upgrade_check c WHERE c.campaign_id = 1021612 GROUP BY c.vehicle_id
) tt

Insert picture description here
Use count (case when …) conditional statistics, combined with temporary flag variables to achieve.

Guess you like

Origin blog.csdn.net/huangdi1309/article/details/97262939