SELFPHP: Version 5.8.2 Befehlsreferenz - Tutorial – Kochbuch – Forum für PHP Einsteiger und professionelle Entwickler

SELFPHP


Professional CronJob-Service

Suche



CronJob-Service    
bei SELFPHP mit ...



 + minütlichen Aufrufen
 + eigenem Crontab Eintrag
 + unbegrenzten CronJobs
 + Statistiken
 + Beispielaufrufen
 + Control-Bereich

Führen Sie mit den CronJobs von SELFPHP zeitgesteuert Programme auf Ihrem Server aus. Weitere Infos



:: Buchempfehlung ::

Fortgeschrittene CSS-Techniken

Fortgeschrittene CSS-Techniken zur Buchempfehlung
 

:: Anbieterverzeichnis ::

Globale Branchen

Informieren Sie sich über ausgewählte Unternehmen im Anbieterverzeichnis von SELFPHP  

 

:: Newsletter ::

Abonnieren Sie hier den kostenlosen SELFPHP Newsletter!

Vorname: 
Name:
E-Mail:
 
 

Zurück   PHP Forum > SELFPHP > MySQLi/PDO/(MySQL)
Hilfe Community Kalender Heutige Beiträge Suchen

MySQLi/PDO/(MySQL) Anfänger, Fortgeschrittene oder Experten können hier Fragen und Probleme rund um MySQLi/PDO/(MySQL) diskutieren

Antwort
 
Themen-Optionen Ansicht
  #1  
Alt 19.05.2008, 18:24:09
VM VM ist offline
Anfänger
 
Registriert seit: May 2008
Alter: 33
Beiträge: 3
Vielfacher JOIN für mein Forum

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 ;

Geändert von VM (19.05.2008 um 18:28:16 Uhr) Grund: tabellen angefügt
Mit Zitat antworten
  #2  
Alt 20.05.2008, 21:00:59
stb stb ist offline
Anfänger
 
Registriert seit: May 2008
Beiträge: 40
AW: Vielfacher JOIN für mein Forum

vieleicht im select einen
MAX(infos.post_time) as dummy,
hinzufügen und dann nochmal mit GROUP BY versuchen...
Mit Zitat antworten
  #3  
Alt 21.05.2008, 16:52:50
Tumasch Tumasch ist offline
Junior Member
 
Registriert seit: Dec 2007
Beiträge: 151
AW: Vielfacher JOIN für mein Forum

LIMIT 6

oder

WHERE themenanzahl > 0
Mit Zitat antworten
  #4  
Alt 21.05.2008, 21:46:58
VM VM ist offline
Anfänger
 
Registriert seit: May 2008
Alter: 33
Beiträge: 3
AW: Vielfacher JOIN für mein Forum

:-)
Der Tip mitm Limit war gut! Danke!

Das mit dem MAX() hat nicht geklappt, aber wahrscheinlich hab ich nur was falsch gemacht.
Wie hätte das aussehen müssen?

So ist meine aktuelle Query:

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
GROUP BY 
themen.thema_id
ORDER BY 
themen.thema_id ASC,
    kategorien.category_order ASC, 	
    infos.post_id ASC
LIMIT 6
Mich würde trotzdem interessieren, wir das mit max() geht, da die 6 im Limit ja konstant ist.
Wenn ich jetzt eine neue Kategorie anlege, müsste ich jedesmal das Skript ändern.

Danke für die bisherige Hilfe!
Mit Zitat antworten
  #5  
Alt 24.05.2008, 02:25:23
Tumasch Tumasch ist offline
Junior Member
 
Registriert seit: Dec 2007
Beiträge: 151
AW: Vielfacher JOIN für mein Forum

Kannst du micht mal hinschreiben was das resultat sein soll?
Mit Zitat antworten
  #6  
Alt 25.05.2008, 00:10:58
VM VM ist offline
Anfänger
 
Registriert seit: May 2008
Alter: 33
Beiträge: 3
AW: Vielfacher JOIN für mein Forum

Das Resultat soll 1 Datensatz je Thema (topic) sein. und nicht 1 datensatz je thread (wie bisher)

Also so:

category | topic | last_thread
1 | 1 | 45
1 | 2 | 65
1 | 3 | 94
2 | 1 | 42
3 | 1 | 10

usw....

Geändert von VM (25.05.2008 um 00:11:28 Uhr)
Mit Zitat antworten
Antwort


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind aus.
[IMG] Code ist aus.
HTML-Code ist aus.

Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Suche Forum mit folgenden funktionen homezonebenny PHP für Fortgeschrittene und Experten 2 04.04.2008 07:36:44
Zwei Aggregatfunktionen in JOIN über drei Tabellen Nico #949494 MySQLi/PDO/(MySQL) 1 29.03.2007 14:47:58
Forum Programmieren flomll PHP Entwicklung und Softwaredesign 19 15.03.2006 03:11:40
Forum mit einer Sortier-Funktion fizzle Off Topic Area 1 25.05.2004 07:52:56
mysql query mit Join problem Silencer PHP für Fortgeschrittene und Experten 4 18.03.2003 15:38:31


Alle Zeitangaben in WEZ +2. Es ist jetzt 21:41:53 Uhr.


Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.


© 2001-2024 E-Mail SELFPHP OHG, info@selfphp.deImpressumKontakt