Hallo,
das ist mein erster Beitrag hier. Gleich eine komplizierte Frage ;-)
In meinem Forum (zu sehen unter
http://www.virtual-meetings.de/forum/) verwende ich folgende Abfrage, um die Forenübersicht zu erzeugen:
(vollständiger code)
Code:
SELECT
kategorien.category_id,
kategorien.category_order,
kategorien.category_name,
themen.thema_id,
themen.thema_name,
threads.thread_name,
userdaten.user_id,
userdaten.username,
infos.post_id,
infos.poster_id,
infos.post_time,
infos.thread_id,
(
SELECT
COUNT(*)
FROM
forum_threads AS threads2
WHERE
threads2.thema_id = threads.thema_id &&
threads2.deleted = 0
) AS themenanzahl,
(
SELECT
COUNT(*)
FROM
forum_post_info AS infos2
INNER JOIN
forum_threads AS threads3 USING(thread_id)
WHERE
threads3.thema_id = threads.thema_id &&
threads3.deleted = 0
) AS beitragsanzahl
FROM
forum_kategorien AS kategorien
LEFT JOIN
forum_themen AS themen USING(category_id)
LEFT JOIN
forum_threads AS threads ON threads.thema_id = themen.thema_id && threads.deleted = 0
LEFT JOIN
forum_post_info AS infos USING(thread_id)
LEFT JOIN
phpbb_users AS userdaten ON userdaten.user_id = infos.poster_id
ORDER BY
kategorien.category_order ASC,
themen.thema_id ASC,
infos.post_id ASC
(code ohne beitragszählung, falls das übersichtlicher ist)
Code:
SELECT
kategorien.category_id,
kategorien.category_order,
kategorien.category_name,
themen.thema_id,
themen.thema_name,
threads.thread_name,
userdaten.user_id,
userdaten.username,
infos.post_id,
infos.poster_id,
infos.post_time,
infos.thread_id
FROM
forum_kategorien AS kategorien
LEFT JOIN
forum_themen AS themen USING(category_id)
LEFT JOIN
forum_threads AS threads ON threads.thema_id = themen.thema_id && threads.deleted = 0
LEFT JOIN
forum_post_info AS infos USING(thread_id)
LEFT JOIN
phpbb_users AS userdaten ON userdaten.user_id = infos.poster_id
ORDER BY
kategorien.category_order ASC,
themen.thema_id ASC,
infos.post_id ASC
Die Abfrage funktioniert, hat aber einen Schönheitsfehler:
Zitat:
1 1 Kategorie 1 1 erstes thema Umfrage 2 Simon 1 2 1209403253 1 4 7
1 1 Kategorie 1 1 erstes thema Second Poll 2 Simon 2 2 1209590364 2 4 7
1 1 Kategorie 1 1 erstes thema Umfrage 2 Simon 3 2 1209590603 1 4 7
1 1 Kategorie 1 1 erstes thema teeeeee 2 Simon 4 2 1209590882 3 4 7
1 1 Kategorie 1 1 erstes thema tfdgfdg 2 Simon 7 2 1210361858 6 4 7
1 1 Kategorie 1 1 erstes thema teeeeee 2 Simon 8 2 1210361877 3 4 7
1 1 Kategorie 1 1 erstes thema teeeeee 2 Simon 9 2 1210602255 3 4 7
1 1 Kategorie 1 2 2. thema in k1 NULL NULL NULL NULL NULL NULL NULL 0 0
2 2 Kategorie 2 3 blaa fd 2 Simon 5 2 1209591006 4 2 2
2 2 Kategorie 2 3 blaa k 2 Simon 6 2 1209591075 5 2 2
3 3 Kategorie 3 4 mmmmmm NULL NULL NULL NULL NULL NULL NULL 0 0
3 3 Kategorie 3 5 kkkkkkkkkkkk NULL NULL NULL NULL NULL NULL NULL 0 0
3 3 Kategorie 3 6 test NULL NULL NULL NULL NULL NULL NULL 0 0
4 4 leere kat NULL NULL NULL NULL NULL NULL NULL NULL NULL 0 0
|
Wie ihr oben seht, kommen nicht wie gewünscht nur 6 Datensätze (für jedes rubrik einen), sondern 14 Datenssätze zurück.
Wenn ich einen neuen Beitrag schreibe, sind es 15.
Ich habe schon probiert, da mit Group By zu arbeiten, dann kriege ich aber nichtmehr den neuesten Beitrag heraus.
Ich hoffe, ich habe mein Problem verständlich erklärt!
Meine Frage ist:
Wie reduziere ich die Menge der Datensätze auf X (x = anzahl der rubriken) und erhalte trotzdem noch den neuesten Beitrag?
Mit freundlichen Grüßen
Simon
---------------------------------------------------------------------------------------------------------------------------
PS:
Hier ist noch der Aufbau der Tabellen, falls der das Problem verständlicher macht.
--
-- Tabellenstruktur für Tabelle `forum_kategorien`
--
CREATE TABLE IF NOT EXISTS `forum_kategorien` (
`category_id` smallint(2) NOT NULL auto_increment,
`category_name` varchar(50) character set utf8 NOT NULL,
`category_order` smallint(2) NOT NULL,
PRIMARY KEY (`category_id`),
KEY `category_ordner` (`category_order`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
--
-- Tabellenstruktur für Tabelle `forum_themen`
--
CREATE TABLE IF NOT EXISTS `forum_themen` (
`thema_id` smallint(2) NOT NULL auto_increment,
`thema_name` varchar(50) character set utf8 NOT NULL,
`category_id` mediumint(8) NOT NULL,
PRIMARY KEY (`thema_id`),
KEY `category_id` (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=7 ;
--
-- Tabellenstruktur für Tabelle `forum_threads`
--
CREATE TABLE IF NOT EXISTS `forum_threads` (
`thread_id` mediumint(8) NOT NULL auto_increment,
`thema_id` mediumint(8) NOT NULL,
`thread_name` varchar(128) character set utf8 NOT NULL,
`thread_locked` tinyint(1) NOT NULL default '0',
`thread_fixed` tinyint(1) NOT NULL default '0',
`deleted` tinyint(1) NOT NULL default '0',
`deltime` varchar(15) collate latin1_german1_ci NOT NULL,
`views` int(10) NOT NULL default '0',
PRIMARY KEY (`thread_id`),
KEY `thema_id` (`thema_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=7 ;
--
-- Tabellenstruktur für Tabelle `forum_post_info`
--
CREATE TABLE IF NOT EXISTS `forum_post_info` (
`post_id` mediumint(8) NOT NULL auto_increment,
`poster_id` mediumint(8) NOT NULL,
`post_time` varchar(15) character set utf8 NOT NULL,
`thread_id` mediumint(8) NOT NULL,
`deleted` tinyint(1) NOT NULL default '0',
`del_time` varchar(15) collate latin1_german1_ci NOT NULL,
`edit_times` int(4) NOT NULL default '0',
`edit_time` varchar(15) collate latin1_german1_ci NOT NULL,
`IP_poster` varchar(15) collate latin1_german1_ci NOT NULL,
PRIMARY KEY (`post_id`),
KEY `thread_id` (`thread_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=11 ;