2011-12-20 4 views
29
(SELECT COUNT(motorbike.`owner_id`) as count,owner.`name`,transport.`type` FROM transport,owner,motorbike WHERE transport.type='motobike' 
AND owner.`owner_id`=motorbike.`owner_id` 
AND transport.`type_id`=motorbike.`motorbike_id` GROUP BY motorbike.owner_id) 
UNION ALL 
(SELECT COUNT(car.`owner_id`) as count,owner.`name`,transport.`type` FROM transport,owner,car WHERE transport.type='car' 
AND owner.`owner_id`=car.`owner_id` 
AND transport.`type_id`=car.`car_id` GROUP BY car.`owner_id`) 

La requête renvoie au-dessus d'un tel résultat ci-dessous,groupe par l'union mysql select requête

count   name 
1    Linda 
2    Mary 
1    Steve 
1    Linda 

Cette requête est de compter combien de transport qui appartient à un propriétaire. Linda ont une voiture et une moto, de sorte que le résultat devrait:

count   name 
2    Linda 
2    Mary 
1    Steve 

J'ai essayé cette requête, mais renvoie une erreur:

(SELECT COUNT(motorbike.`owner_id`),owner.`name`,transport.`type` FROM transport,owner,motorbike WHERE transport.type='motobike' 
AND owner.`owner_id`=motorbike.`owner_id` 
AND transport.`type_id`=motorbike.`motorbike_id`) 
UNION ALL 
(SELECT COUNT(car.`owner_id`),owner.`name`,transport.`type` FROM transport,owner,car WHERE transport.type='car' 
AND owner.`owner_id`=car.`owner_id` 
AND transport.`type_id`=car.`car_id`) GROUP BY motorbike.owner_id 

Quelqu'un peut-il me aider s'il vous plaît?

Répondre

85
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 
+0

Son travail .. Merci beaucoup;) – user1103332

+0

Une façon simple de le faire, mais je n'aurais jamais pensé à cela. Impressionnant! – alanaktion

+0

Comment ajoutez-vous l'ordre et la limite à? –

1

Essayez cette ÉDITÉ:

(SELECT COUNT(motorbike.owner_id),owner.name,transport.type FROM transport,owner,motorbike WHERE transport.type='motobike' AND owner.owner_id=motorbike.owner_id AND transport.type_id=motorbike.motorbike_id GROUP BY motorbike.owner_id) 

UNION ALL 

(SELECT COUNT(car.owner_id),owner.name,transport.type FROM transport,owner,car WHERE transport.type='car' AND owner.owner_id=car.owner_id AND transport.type_id=car.car_id GROUP BY car.owner_id) 
+0

j'ai essayé groupe par car.owner_id, mais retourne toujours erreur – user1103332

+0

Quelle est l'erreur vous obtenez? Si j'ai le texte d'erreur, je peux mieux déboguer le problème –

+0

Code d'erreur: 1064 C'est erreur que je reçois. Vous avez une erreur dans votre syntaxe SQL; consultez le manuel qui correspond à votre version du serveur MySQL pour la bonne syntaxe à utiliser près de 'GROUP BY car.owner_id – user1103332

3

Cela peut être ce que votre après:

SELECT Count(Owner_ID), Name 
FROM (
    SELECT M.Owner_ID, O.Name, T.Type 
    FROM Transport As T, Owner As O, Motorbike As M 
    WHERE T.Type = 'Motorbike' 
    AND O.Owner_ID = M.Owner_ID 
    AND T.Type_ID = M.Motorbike_ID 

    UNION ALL 

    SELECT C.Owner_ID, O.Name, T.Type 
    FROM Transport As T, Owner As O, Car As C 
    WHERE T.Type = 'Car' 
    AND O.Owner_ID = C.Owner_ID 
    AND T.Type_ID = C.Car_ID 
) 
GROUP BY Owner_ID