Skip to content

Instantly share code, notes, and snippets.

@FernandoBasso
Last active April 11, 2017 17:31
Show Gist options
  • Save FernandoBasso/db320f4a93013cea84bd27f9a043a34d to your computer and use it in GitHub Desktop.
Save FernandoBasso/db320f4a93013cea84bd27f9a043a34d to your computer and use it in GitHub Desktop.
Categories and Subcategories (MariaDB)
DESCRIBE categorias;
SELECT CONCAT (
t1.nome
, IFNULL (CONCAT (' → ', t2.nome), '')
, IFNULL (CONCAT (' → ', t3.nome), '')
, IFNULL (CONCAT (' → ', t4.nome), '')
) AS fullpath
FROM categorias AS t1
LEFT JOIN categorias AS t2 ON t2.parent_id = t1.id
LEFT JOIN categorias AS t3 ON t3.parent_id = t2.id
LEFT JOIN categorias AS t4 ON t4.parent_id = t3.id
WHERE t1.parent_id IS NULL;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| parent_id | int(11) | YES | MUL | NULL | |
| nome | varchar(32) | NO | | NULL | |
| descricao | varchar(256) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
+--------------------------------------------------------+
| fullpath |
+--------------------------------------------------------+
| Porta Retrato → Onda |
| Porta Retrato → Mozaico |
| Porta Retrato → Maré → Maré Damas |
| Porta Retrato → Antigue |
| Porta Retrato → Frases Quadrados |
| Porta Retrato → Frases Linhas |
| Porta Retrato → Clips |
| Porta Retrato → Teen |
| Porta Retrato → Cursos |
| Porta Retrato → Diversos |
| Porta Retrato → Formatura |
| Porta Retrato → Aniversário |
| Porta Retrato → Amor |
| Porta Retrato → Coração → Mamãe → Com o Filho |
| Painel de Fotos → Amor |
| Painel de Fotos → Mãe |
| Kit Chimarrão |
+--------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment