Group by with union mysql select query

前端 未结 3 918
忘掉有多难
忘掉有多难 2020-12-08 06:02
(SELECT COUNT(motorbike.`owner_id`) as count,owner.`name`,transport.`type` FROM transport,owner,motorbike WHERE transport.type=\'motobike\'
AND owner.`owner_id`=moto         


        
3条回答
  •  囚心锁ツ
    2020-12-08 07:04

    select sum(qty), name
    from (
        select count(m.owner_id) as qty, o.name
        from transport t,owner o,motorbike m
        where t.type='motobike' and o.owner_id=m.owner_id
            and t.type_id=m.motorbike_id
        group by m.owner_id
    
        union all
    
        select count(c.owner_id) as qty, o.name,
        from transport t,owner o,car c
        where t.type='car' and o.owner_id=c.owner_id and t.type_id=c.car_id
        group by c.owner_id
    ) t
    group by name
    

提交回复
热议问题