[mysql/Joins] Count telt verkeerd

Pagina: 1
Acties:

  • Vulpecula
  • Registratie: April 2001
  • Laatst online: 18-08 21:00
Ik wil ten eerste ff mijn excuses aanbieden voor het openen van dit nutteloze topicje. Ik hoop dat mijn excuses wordt geaccepteerd.

Ik heb nu een serieus vraag. Ik ben bezig met het maken van een eigen forum. Ik heb tot nu toe alles nog uit een query kunnen halen. Ik wil net als hier bij tweakers het aantal topics weergeven en het aantal reply's. Ik heb nu 2 replys en 1 topic in de database staan. Maar hij geeft telkens 2 aan bij topics. Dit komt denk ik omdat hij twee keer t.topic_id nodig heeft voor r.reply_id

PHP:
1
2
3
4
5
6
7
8
9
10
$query = "
SELECT 
  b.board_id AS bid, c.category_id AS cid, b.title AS btitle, b.description AS bdescription, c.title AS ctitle, count(t.topic_id) AS tcount, max(date_format(t.post_date,'%m-%d-%Y @ %H:%i')) AS post_date, count(r.reply_id) AS rcount
FROM forum_board AS b 
  LEFT JOIN forum_category AS c USING (category_id) 
  LEFT JOIN forum_topic AS t ON b.board_id=t.board_id
  LEFT JOIN forum_reply AS r ON r.topic_id=t.topic_id
GROUP BY b.board_id
ORDER BY c.ord ASC, b.ord ASC
";

Dus ik krijg nu
code:
1
2
Forum                           Topics                            Replies            Laatste post 
Ruimtevaart                      2                                   2                  11-19-2002 @ 16:11


terwijl in de database maar 1 topic is.

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

count(distinct(topicid)) wellicht

Verwijderd

volgens mij moet je bij een sum,count, max ed in de query de rest groupen om alles juist terug te krijgen bv zo

PHP:
1
2
3
4
5
6
7
8
9
10
$query = "
SELECT 
  b.board_id AS bid, c.category_id AS cid, b.title AS btitle, b.description AS bdescription, c.title AS ctitle, count(t.topic_id) AS tcount, max(date_format(t.post_date,'%m-%d-%Y @ %H:%i')) AS post_date, count(r.reply_id) AS rcount
FROM forum_board AS b 
  LEFT JOIN forum_category AS c USING (category_id) 
  LEFT JOIN forum_topic AS t ON b.board_id=t.board_id
  LEFT JOIN forum_reply AS r ON r.topic_id=t.topic_id
GROUP BY b.board_id,c.category_id,b.title,b.description,c.title,max(date_format(t.post_date,'%m-%d-%Y @ %H:%i')) ( of post_date)
ORDER BY c.ord ASC, b.ord ASC
";



je zou het ook met een desctinct select kunnen proberen