AW: NESTED SETS in XML - Gruppierung - Zusammenfassung
Zuerst möchte ich mich bei Dir bedanken, dass Du deine Zeit und SQL-Wissen zur Verfügung stellst.
Nochmals Danke für die Hilfe
Da die meisten Thread mit einem "ich kenne mich jetzt aus" ohne Zusammenfassung Enden, hier alles Notwendige für den Interessierten.
Grupperung von Nested Sets: Vorteil bei bei UPDATE da nicht der komplette Baum abgearbeitet wird. Bzw weniger Konflikte bei mehreren USER.
(besteht aus Oberelement/Unterelemente)
Lösung 1 (reine Lehre) mit Testdaten
Code:
CREATE TABLE IF NOT EXISTS `tree` (
`id` int(11) NOT NULL,
`tag` varchar(100) NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
`desc` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Daten für Tabelle `tree`
--
INSERT INTO `tree` (`id`, `tag`, `lft`, `rgt`, `desc`) VALUES
(1, 'A', 1, 16, 'content A'),
(2, 'B', 2, 3, 'content B'),
(3, 'C', 4, 13, 'content C'),
(4, 'D', 5, 12, 'content D'),
(5, 'E', 6, 7, 'content E'),
(6, 'F', 8, 11, 'content F'),
(7, 'G', 9, 10, 'content G'),
(8, 'H', 14, 15, 'content H'),
(9, 'G2', 9, 10, 'content G1'),
(10, 'G3', 9, 10, 'content G2'),
(11, 'C1', 4, 13, 'content C1');
SET @a=0;
SELECT CASE flag
WHEN 1 THEN
IF(@a=t.lft,
CONCAT( '<' , t.tag, '>', t.`desc`,'<' , t.tag, '>'),
CONCAT( '<' , t.tag, '>', t.`desc`))
WHEN 2 THEN
CONCAT( '</' , t.tag, '>')
END AS xml_teil,
@a:=t.lft
FROM ( SELECT tag, 1 AS flag, lft AS pt, id
FROM tree
UNION ALL
SELECT tag, 2 AS flag, rgt AS pt, id
FROM tree
GROUP BY pt
ORDER BY pt,id) r
JOIN tree t
ON t.id = r.id
ORDER BY r.pt;
Lösung 2 (Trennen von baumstruktur und Content) mit Testdaten
(Oberelemente 1/Unterelemente2)
Code:
CREATE TABLE IF NOT EXISTS `tree` (
`id` int(11) NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Daten für Tabelle `tree`
--
INSERT INTO `tree` (`id`, `lft`, `rgt`) VALUES
(1, 1, 16),
(2, 2, 3),
(3, 4, 13),
(4, 5, 12),
(5, 6, 7),
(6, 8, 11),
(7, 9, 10),
(8, 14, 15);
CREATE TABLE IF NOT EXISTS `element` (
`id` int(11) NOT NULL,
`tag` varchar(100) NOT NULL,
`desc` text NOT NULL,
`id_tree` int(11) NOT NULL,
ebene_id SMALLINT NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Daten für Tabelle `element`
--
INSERT INTO `element` (`id`, `tag`, `desc`, `id_tree`, ebene_id) VALUES
(1, 'A', 'content A', 1, 1),
(2, 'B', 'content B', 2, 1),
(3, 'C', 'content C', 3, 1),
(4, 'D', 'content D', 4, 1),
(5, 'E', 'content E', 5, 1),
(6, 'F', 'content F', 6, 1),
(7, 'G', 'content G', 7, 1),
(8, 'H', 'content H', 8, 1),
(9, 'G2', 'content G1', 7, 2),
(10, 'G3', 'content G2', 7, 2),
(11, 'C1', 'content C1', 3, 2);
SELECT CASE r.flag
WHEN 1 THEN CONCAT( '<' , r.tag, '>', r.`desc` )
WHEN 2 THEN CONCAT( '</' , r.tag, '>')
WHEN 3 THEN CONCAT( '<' , r.tag, '>', r.`desc`, '</' , r.tag, '>')
END AS xml_teil
FROM (SELECT e.tag, 1 AS flag, t.lft AS pt, e.`desc`
FROM tree t
JOIN element e
ON e.id_tree = t.id
WHERE e.ebene_id = 1
UNION ALL
SELECT e.tag, 2 AS flag, t.rgt AS pt, e.`desc`
FROM tree t
JOIN element e
ON e.id_tree = t.id
WHERE e.ebene_id = 1
UNION ALL
SELECT e.tag, 3 AS flag, t.lft AS pt, e.`desc`
FROM tree t
JOIN element e
ON e.id_tree = t.id
WHERE e.ebene_id = 2
) r
ORDER BY r.pt, r.tag;
Links für Nested Sets
http://www.bittermonk.com/
http://joecelkothesqlapprentice.blog...?q=nested+sets
http://www.baoxuehao.cn/index.php/archives/484
http://www.klempert.de/nested_sets/
http://phpperformance.de/nested-sets...eume-in-mysql/
grüsse helmut
Ich würde mich freuen wenn Du mir noch bei Teil 3 meines Problem helfen könntest.
http://www.selfphp.de/forum/showthre...636#post138636
Geändert von mr_sol (21.10.2010 um 11:57:51 Uhr)
|