|
Source code
|
1
2
3
4
5
6
7
8
9
|
SELECT
A.`id` AS `id`,
SUM(`stat_topics`) AS `sum_stat_topics`,
SUM(`stat_posts`) AS `sum_stat_posts`
FROM `sj_forums` A
LEFT JOIN `sj_forums` B ON B.id = A.sub
LEFT JOIN `sj_forums` C ON C.id = B.sub
WHERE A.`sub`=0
GROUP BY `id`
|
#1052 - Column 'stat_topics' in field list is ambiguous
Ich fasse mein Problem mal so...
Ich habe eine Tabelle categories, wo jede Zeile eine Kategorie darstellt.
Eine Kategorie kann in einer anderen geschachtelt sein.
Jede Kategorie hat eine eindeutige id und eine sub-Spalte, welche angibt,
welcher übergeordneten Kategorie sie unetrgeordnet ist (Vaterzeiger).
Außerdem sind da noch die 2 Spalten 'title', 'text' und 'hits'.
In text ist halt der Text, den man sich anschaut.
hits gibt an, wie oft die ejweilige Kategorie gelesen wurde.
Die Wurzelkategorie hat die id 0 und sub -1.
Nun möchte ich alle Kategorien mit ihrer id abrufen und der absoluten
Anzahl der hits, die der Kategorie der id 0 (Wurzelkategorie)
untergeordnet sind. In einer Gruppierung halt, mit SUM() etc.
Unter absolute Anzahl meine ich die Summe aus der
hits-Spalte einer aufgelisteten Unterkategorie und
denen, die dieser untergeordnet sind.
Der Baum kann auf 3 Ebenen begrenzt sein.
Dann müsste das mit JOINS irgendwie zu machen sein.
Nur weiß ich halt nicht, wie..
So klappt das schonmal nicht:
|
Source code
|
1
2
3
4
5
6
|
SELECT `id`, SUM(B.`hits`)
FROM `categories` A, `categories` B, `categories` C
WHERE A.`sub`=0
LEFT JOIN B.`sub`=A.`id`
LEFT JOIN C.`sub`=B.`id`
GROUP BY `id`
|
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN B. `sub` = A. `id` LEFT JOIN C. `sub` = B. `id` GROUP BY `id`
LIMI' at line 1
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
-- phpMyAdmin SQL Dump
-- version 2.11.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Erstellungszeit: 25. Dezember 2007 um 23:59
-- Server Version: 5.0.45
-- PHP-Version: 5.2.4
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Datenbank: `test`
--
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `categories`
--
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL auto_increment,
`sub` int(11) NOT NULL default '0',
`hits` int(10) unsigned NOT NULL default '0',
`title` text collate latin1_general_ci NOT NULL,
`text` text collate latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=3 ;
--
-- Daten für Tabelle `categories`
--
INSERT INTO `categories` (`id`, `sub`, `hits`, `title`, `text`) VALUES
(1, 0, 0, 'Spielen nach Noten', 'Wenn sie bla bla können sie hier ja weitermachen\r\nhier das bla bla usw...'),
(0, -1, 0, 'Gitarrenkurs für Anfänger', 'Hier bla bla Gitarren bla\r\nFahren sie fort mit Spielen nach Noten bla\r\nwenn bla'),
(2, 0, 0, 'Spielen nach Gefühl', 'Ich muss euch ab hier enttäuschen.\r\nIch bin ein lügner, ich kann selbst nicht Gitarre spielen...');
|
Das hier funtkioniert mit der Ebenenbegrenzung, die ich leider in Kauf nehmen muss.
|
Source code
|
1
2
3
4
5
6
7
|
SELECT
A.id AS `id`,
A.title AS `title`,
SUM(A.stat_posts) + SUM(B.stat_posts) + SUM(C.stat_posts) AS `sum_stat_posts`
FROM `sj_forums` A, `sj_forums` B, `sj_forums` C
WHERE A.sub=0 AND B.sub=A.id AND C.sub=B.id
GROUP BY A.id
|
Leider aber auch nur auf Unterforen, die selbst mindestens 2 Unterforen haben!