具有多个连接的mysql面包屑

问题描述 投票:0回答:1

我想选择所有活动类别并使用面包屑进行查看。在我的数据库中,我有以下内容:

id_类别 类别名称 velleman_id velleman_parent prestashop_category prestashop_category_2 prestashop_category_3 prestashop_category_4 已删除
1 汽车和自行车 10 0
2 自行车 10.10 10 0
3 汽车 10.15 10 0
4 电动垫。 15 0
5 电线 10.15 15 0
6 连接器 15.15 15 0
7 10.15.15 15.15 0
8 圆形 15.15.10.10 10.15.15 0

我的查询的问题是它只显示 velleman_id 包含 4 个组的类别(如 id_category 8)。

SELECT
    IFNULL(c4.id_category, IFNULL(c3.id_category, IFNULL(c2.id_category, IFNULL(c1.id_category, '')))) AS id_category,
    IFNULL(c1.velleman_id, '') AS velleman_id,
    IFNULL(c2.velleman_id, '') AS velleman_id,
    IFNULL(c3.velleman_id, '') AS velleman_id,
    IFNULL(c4.velleman_id, '') AS velleman_id,
    CONCAT(IFNULL(c1.category_name, ''), CONCAT(' > ', c2.category_name), CONCAT(' > ', c3.category_name), CONCAT(' > ', c4.category_name)) AS category_name,
    IFNULL(c4.prestashop_category, IFNULL(c3.prestashop_category, IFNULL(c2.prestashop_category, c1.prestashop_category))) AS prestashop_category,
    IFNULL(c4.prestashop_category_2, IFNULL(c3.prestashop_category_2, IFNULL(c2.prestashop_category_2, c1.prestashop_category_2))) AS prestashop_category_2,
    IFNULL(c4.prestashop_category_3, IFNULL(c3.prestashop_category_3, IFNULL(c2.prestashop_category_3, c1.prestashop_category_3))) AS prestashop_category_3,
    IFNULL(c4.prestashop_category_4, IFNULL(c3.prestashop_category_4, IFNULL(c2.prestashop_category_4, c1.prestashop_category_4))) AS prestashop_category_4,
    IFNULL(c4.deleted, IFNULL(c3.deleted, IFNULL(c2.deleted, c1.deleted))) AS deleted
FROM
    velleman_categories c1
LEFT JOIN velleman_categories c2 ON
    c2.velleman_parent = c1.velleman_id
LEFT JOIN velleman_categories c3 ON
    c3.velleman_parent = c2.velleman_id
LEFT JOIN velleman_categories c4 ON
    c4.velleman_parent = c3.velleman_id
WHERE
    c1.velleman_parent = 0;

尝试在同一个表上使用 IFNULL 和左联接进行选择,但参考以前的联接。

当我选择所有活动行时,我有 523 行:

SELECT * FROM velleman_categories WHERE deleted = 0;

通过上面的查询,我有 423 行。 这就是我想要得到的:

id_类别 velleman_id velleman_id velleman_id velleman_id 类别名称 prestashop_category prestashop_category_2 prestashop_category_3 prestashop_category_4
4 15 (空或为空) (空或为空) (空或为空) 电动垫。 (空) (空) (空) (空)
8 15 15.15 10.15.15 15.15.10.10 电动垫。 > 连接器 > 公头 > 圆形 (空) (空) (空) (空)

我认为问题在于

JOIN
,有些父母没有价值观,或者可能没有根类别。

mysql join concatenation breadcrumbs ifnull
1个回答
0
投票

您需要修改 JOIN 中的条件,并且需要添加基表 (c)。

这是工作查询:

SELECT
IFNULL(c4.id_category, IFNULL(c3.id_category, IFNULL(c2.id_category, IFNULL(c1.id_category, '')))) AS id_category,
IFNULL(c1.velleman_id, '') AS velleman_id,
IFNULL(c2.velleman_id, '') AS velleman_id,
IFNULL(c3.velleman_id, '') AS velleman_id,
IFNULL(c4.velleman_id, '') AS velleman_id,
CONCAT(IFNULL(c1.category_name, ''), CONCAT(' > ', c2.category_name), CONCAT(' > ', c3.category_name), CONCAT(' > ', c4.category_name)) AS category_name,
IFNULL(c4.prestashop_category, IFNULL(c3.prestashop_category, IFNULL(c2.prestashop_category, c1.prestashop_category))) AS prestashop_category,
IFNULL(c4.prestashop_category_2, IFNULL(c3.prestashop_category_2, IFNULL(c2.prestashop_category_2, c1.prestashop_category_2))) AS prestashop_category_2,
IFNULL(c4.prestashop_category_3, IFNULL(c3.prestashop_category_3, IFNULL(c2.prestashop_category_3, c1.prestashop_category_3))) AS prestashop_category_3,
IFNULL(c4.prestashop_category_4, IFNULL(c3.prestashop_category_4, IFNULL(c2.prestashop_category_4, c1.prestashop_category_4))) AS prestashop_category_4,
IFNULL(c4.deleted, IFNULL(c3.deleted, IFNULL(c2.deleted, c1.deleted))) AS deleted
FROM velleman_categories c
LEFT JOIN velleman_categories c1 ON SUBSTRING_INDEX(c.velleman_id, '.', 1) = c1.velleman_id
LEFT JOIN velleman_categories c2 ON SUBSTRING_INDEX(c.velleman_id, '.', 2) = c2.velleman_id AND LENGTH(c.velleman_id) - LENGTH(REPLACE(c.velleman_id, '.', '')) > 0
LEFT JOIN velleman_categories c3 ON SUBSTRING_INDEX(c.velleman_id, '.', 3) = c3.velleman_id AND LENGTH(c.velleman_id) - LENGTH(REPLACE(c.velleman_id, '.', '')) > 1
LEFT JOIN velleman_categories c4 ON SUBSTRING_INDEX(c.velleman_id, '.', 4) = c4.velleman_id AND LENGTH(c.velleman_id) - LENGTH(REPLACE(c.velleman_id, '.', '')) > 2

结果: here

© www.soinside.com 2019 - 2024. All rights reserved.