Toon posts:

[mysql] Count van left join

Pagina: 1
Acties:

Verwijderd

Topicstarter
Hallo,

Ik heb 2 tabellen; storingsrapporten en notities. De relatie is 1:n (1 storingsrapport kan meerdere notities hebben).

Nu wil ik alle stroringrapporten ophalen (easy), maar daarbij ook het aantal notities per rapport.
Ikzelf dacht aan het volgende:
PHP:
1
2
3
4
5
6
7
8
9
10
SELECT
 s.*,
 COUNT( n.id ) AS aantalNotities
FROM
 storingsrapporten AS s,
 notities AS n 
WHERE
 n.rapport = s.id 
GROUP BY
 n.id


n.rapport bevat het id van het storingsrapport waar het bij hoor (s.id).

Maar nu krijg ik volgens mij het totaal aan notities terug van de rapporten, en niet per rapport.

Welke richting moet ik op zoeken?

Alvast bedankt!

Roemer

  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 19-05 09:52

thomaske

» » » » » »

Je doet nu geen left join, maar een normale join. Een left join zal namelijk het resultaat geven wat je wilt. Zoek even op mysql.com naar de syntax.

[goede-bui-modus]
JOIN Syntax
[/goede-bui-modus]

[ Voor 27% gewijzigd door thomaske op 07-12-2004 12:03 ]

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."


  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 19-05 07:31

Janoz

Moderator Devschuur®

!litemod

Daarnaast moet je groub by niet op je notities doen, maar op alle velden van de storingsmelding. Je wilt het immers gegroepeerd per storingsmelding ;)

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


  • Haploid
  • Registratie: Maart 2002
  • Laatst online: 29-12-2021

Haploid

Doh!

Je zoekt meer iets in de trant van een subquery, nl.:
SQL:
1
2
3
4
SELECT
    s.*,
    (SELECT COUNT(*) FROM notities AS n WHERE n.rapport=s.id) AS aantalNotities
FROM storingsrapporten AS s

edit:

Oh ja, en voordat daar weer een discussie over begint, dit werkt alleen op MySQL 4.1+.

[ Voor 22% gewijzigd door Haploid op 07-12-2004 12:13 ]

Hey, I came here to be drugged, electrocuted and probed, not insulted.


  • stp_4
  • Registratie: Maart 2003
  • Laatst online: 18-05 16:28
Dit zou ook kunnen werken denk ik

code:
1
2
3
4
5
6
7
8
9
SELECT 
       storingsrapport.id,
       COUNT(notaties.id)  AS aantalNotaties
    FROM 
        storingsrapport, notaties
    WHERE
        notaties.rapport = storingsrapport.id
    GROUP BY
        storingsrapport.id

stp - PSN ID: stp_4


  • thomaske
  • Registratie: Juni 2000
  • Laatst online: 19-05 09:52

thomaske

» » » » » »

stp_4 schreef op dinsdag 07 december 2004 @ 12:13:
Dit zou ook kunnen werken denk ik

code:
1
2
3
4
5
6
7
8
9
SELECT 
       storingsrapport.id,
       COUNT(notaties.id)  AS aantalNotaties
    FROM 
        storingsrapport, notaties
    WHERE
        notaties.rapport = storingsrapport.id
    GROUP BY
        storingsrapport.id
Nee, want deze toont alleen de storingsrapporten met 1 of meer notities. En niet degene met 0 notities.

Brusselmans: "Continuïteit bestaat niet, tenzij in zinloze vorm. Iets wat continu is, is obsessief, dus ziekelijk, dus oninteressant, dus zinloos."

Pagina: 1