Einzelnen Beitrag anzeigen
  #8  
Alt 14.11.2010, 21:11:46
thomas_w thomas_w ist offline
Junior Member
 
Registriert seit: Aug 2010
Alter: 14
Beiträge: 395
AW: Reihenfolge nach JOIN / GROUP BY festlegen

Zwei mögliche Lösungswege

a) WHERE erweitern mit IN()...

Code:
SELECT c.id AS c_id, 
       MAX(c.title) AS c_title, 
       MAX(a.title) AS a_titel, 
       MAX(a.modified) AS a_modified,
       MAX(a.publish_up) AS a_puplish_up,
       MAX(s.title) AS s_title, 
       MAX(s.id) AS s_id 
  FROM categories c 
  JOIN content a 
    ON a.catid=c.id
  JOIN sections s 
    ON c.section=s.id
WHERE s.id IN (3,4,5)
GROUP BY c.id
ORDER BY a_puplish_up DESC ;

b) Mehrere SQL verbinden mit UNION ALL

Code:
SELECT c.id AS c_id, 
       MAX(c.title) AS c_title, 
       MAX(a.title) AS a_titel, 
       MAX(a.modified) AS a_modified,
       MAX(a.publish_up) AS a_puplish_up,
       MAX(s.title) AS s_title, 
       MAX(s.id) AS s_id 
  FROM categories c 
  JOIN content a 
    ON a.catid=c.id
  JOIN sections s 
    ON c.section=s.id
WHERE s.id = 3
GROUP BY c.id

UNION ALL

SELECT c.id AS c_id, 
       MAX(c.title) AS c_title, 
       MAX(a.title) AS a_titel, 
       MAX(a.modified) AS a_modified,
       MAX(a.publish_up) AS a_puplish_up,
       MAX(s.title) AS s_title, 
       MAX(s.id) AS s_id 
  FROM categories c 
  JOIN content a 
    ON a.catid=c.id
  JOIN sections s 
    ON c.section=s.id
WHERE s.id = 4
GROUP BY c.id

UNION ALL

SELECT c.id AS c_id, 
       MAX(c.title) AS c_title, 
       MAX(a.title) AS a_titel, 
       MAX(a.modified) AS a_modified,
       MAX(a.publish_up) AS a_puplish_up,
       MAX(s.title) AS s_title, 
       MAX(s.id) AS s_id 
  FROM categories c 
  JOIN content a 
    ON a.catid=c.id
  JOIN sections s 
    ON c.section=s.id
WHERE s.id = 5
GROUP BY c.id


ORDER BY a_puplish_up DESC ;

Viel Erfolg!

Grüße
Thomas
__________________
Die SQL-Backstube
Bietet Rezepte, Lösungen und ausführliche Beispiele rund um gesundes SQL und zufriedene Datenbanken.
Mit Zitat antworten