Fandt et workaround:
SELECT SUM(photos.views), exhibitions.exhibitionid
FROM photos,
exhibitions,
photoisexhibition
WHERE photos.photoid = photoisexhibition.photoid
AND photoisexhibition.exhibitionid = exhibitions.exhibitionid
AND exhibitions.ispublic = 1
AND photos.photographerid = 1
GROUP BY exhibitions.exhibitionid
Viser views pr. exhibition som er ens for alle exhibitions.
// Mads
> From: mads@imv.au.dk (Mads Pedersen)
> Organization:
http://groups.google.com/
> Newsgroups: dk.edb.database
> Date: 12 Jan 2003 10:33:53 -0800
> Subject: MySQL - SUM query problem
>
> På mit foto-site vil jeg gerne sammentælle det antal gange en given
> fotografs billeder er blevet vist.
>
> Jeg har prøvet flg:
>
> SELECT SUM(photos.views)
> FROM photos, exhibitions, photoisexhibition
> WHERE photos.photoid=photoisexhibition.photoid
> AND photoisexhibition.exhibitionid=exhibitions.exhibitionid
> AND exhibitions.ispublic='1'
> AND photos.photographerid='1";
>
> Sammentællingen virker fint, men hvis et billede f.eks. tilhører 2
> exhibitions så tæller antallet af views dobbelt - og det er ikke
> meningen.
>
> Er der et lille fix som jeg kan lave (noget med DISTINCT?) eller har
> jeg grebet query'en helt forkert an?
>
> Som I nok kan se er jeg SQL nybegynder så "avancerede" løsningsforslag
> må gerne forklares
>
> Mange hilsner,
>
> // Mads