Moin!
Muss jetzt aufgegeben und vorweg: ich bin SQL-Einsteiger.
Habe die u. a. Tabellen und möchte eine Abfrage mit folgendem Ergebnis erstellen:
product_id | Name der Kategorie Level 1 | Name der Kategorie Level 2 | Name der Kategorie Level 3 | Name der Kategorie Level 4
Die zeilenweise vorhandene Zuordnung product_id <-> category_id muss je nach Level in einzelne Spalten transponiert werden.
Es klappt bis zwei Ebenen mit dieser Abfrage:
Code:
SELECT DISTINCT *
FROM
(SELECT
t_pcp_1.product_id AS cat1_product_id,
t_pc_1.level AS cat1_level,
t_pcp_1.category_id AS cat1_category_id,
t_pc_1.name_de AS cat1_name
FROM (t_product_categories_products t_pcp_1 JOIN t_product_categories t_pc_1 ON ((t_pcp_1.category_id = t_pc_1.id)))
WHERE (t_pc_1.level = 1 )) AS t_cat_1
LEFT OUTER JOIN
(SELECT
t_pcp_2.product_id AS cat2_product_id,
# t_pc_2.level AS cat2_level,
# t_pcp_2.category_id AS cat2_category_id,
t_pc_2.name_de AS cat2_name
FROM (t_product_categories_products t_pcp_2 JOIN t_product_categories t_pc_2 ON ((t_pcp_2.category_id = t_pc_2.id)))
WHERE (t_pc_2.level = 2 )) AS t_cat_2
ON t_cat_1.cat1_product_id = t_cat_2.cat2_product_id
# LEFT OUTER JOIN
# (SELECT
# t_pcp_3.product_id AS cat3_product_id,
# t_pc_3.level AS cat3_level,
# t_pcp_3.category_id AS cat3_category_id,
# t_pc_3.name_de AS cat3_name
# FROM (t_product_categories_products t_pcp_3 JOIN t_product_categories t_pc_3 ON ((t_pcp_3.category_id = t_pc_3.id)))
# WHERE (t_pc_3.level = 2 )) AS t_cat_3
# ON t_cat_1.cat1_product_id = t_cat_3.cat3_product_id
ORDER BY t_cat_1.cat1_product_id
ab der 3. Ebene kommt dann die Fehlermeldung:
###
The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
###
Kann man das überhaupt bei diesem Tabellenaufbau in einer Abfrage darstellen?
Bin für Tipps und Hilfe dankbar.
Vielen Dank dafür im voraus!
Code:
--
-- Tabellenstruktur für Tabelle `t_product_categories`
--
CREATE TABLE IF NOT EXISTS `t_product_categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name_de` varchar(255) NOT NULL,
`name_en` varchar(255) NOT NULL,
`color` varchar(10) NOT NULL,
`parent` int(10) unsigned NOT NULL,
`level` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- Daten für Tabelle `t_product_categories`
--
INSERT INTO `t_product_categories` (`id`, `name_de`, `name_en`, `color`, `parent`, `level`) VALUES
(1, 'Kategorie1', 'Category1', '#007d40', 0, 1),
(2, 'Kategorie1.1', 'category1.1', '', 1, 2),
(3, 'Kategorie1.1.1', 'category1.1.1', '', 2, 3),
(4, 'Kategorie1.1.2', 'category1.1.2', '', 2, 3),
(5, 'Kategorie1.1.1.1', 'category1.1.1.1', '', 1, 4);
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `t_product_categories_products`
--
CREATE TABLE IF NOT EXISTS `t_product_categories_products` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(11) unsigned NOT NULL,
`product_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
--
-- Daten für Tabelle `t_product_categories_products`
--
INSERT INTO `t_product_categories_products` (`id`, `category_id`, `product_id`) VALUES
(1, 1, 2),
(2, 2, 2),
(3, 3, 2),
(4, 5, 2),
(5, 1, 2),
(6, 2, 2),
(7, 4, 2);