I would like to select all active categories and view it with a breadcrumb. In my database I have the following:
id_category | category_name | velleman_id | velleman_parent | prestashop_category | prestashop_category_2 | prestashop_category_3 | prestashop_category_4 | deleted |
---|---|---|---|---|---|---|---|---|
1 | Car & Bike | 10 | 0 | |||||
2 | Bike | 10.10 | 10 | 0 | ||||
3 | Car | 10.15 | 10 | 0 | ||||
4 | Electric mat. | 15 | 0 | |||||
5 | Wires | 15.10 | 15 | 0 | ||||
6 | Connectors | 15.15 | 15 | 0 | ||||
7 | Male | 15.15.10 | 15.15 | 0 | ||||
8 | Round | 15.15.10.10 | 15.15.10 | 0 |
The problem with my query is that it only shows categories where velleman_id contains 4 groups (like in 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;
Tried to select with IFNULL and left join on the same table but with reference to previous joins.
When I select all active rows, I have 523 rows:
SELECT * FROM velleman_categories WHERE deleted = 0;
And with the query above I have 423 rows.
This is what I like to get:
id_category | velleman_id | velleman_id | velleman_id | velleman_id | category_name | prestashop_category | prestashop_category_2 | prestashop_category_3 | prestashop_category_4 |
---|---|---|---|---|---|---|---|---|---|
4 | 15 | (empty or null) | (empty or null) | (empty or null) | Electric mat. | (empty) | (empty) | (empty) | (empty) |
8 | 15 | 15.15 | 15.15.10 | 15.15.10.10 | Electric mat. > Connectors > Male > Round | (empty) | (empty) | (empty) | (empty) |
I think the problem is with the JOIN
and that some parents have no values or maybe a root category.
2
Answers
You need to modify the condition in your JOIN, add the base table (c), and change the CONCAT for the category_name.
Here is the working query:
Result:
here
I am using the fact that
CONCAT(null,'text')
returnsnull
, so I need not useIFNULL
:output:
see: DBFIDDLE