Last active
December 26, 2016 13:56
-
-
Save picodotdev/7156980 to your computer and use it in GitHub Desktop.
Relaciones jerárquicas en bases de datos relacionales
http://elblogdepicodev.blogspot.com/2013/10/relaciones-jerarquicas-en-bases-de-datos-relacionales.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select c.id as id | |
from Categoria as c, Categoria as p | |
where c.left between p.left and p.right | |
and c.id = ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select c.id as id | |
from Categoria as c, Categoria as p | |
where c.left > p.left and c.right < p.right | |
and p.id = ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select c10.id as id10, c9.id as id9, c8.id as id8, c7.id as id7, c6.id as id6, c5.id as id5, c4.id as id4, c3.id as id3, c2.id as id2, c1.id as id1 | |
from categoria c1 | |
left join categoria c2 on c2.id = c1.categoria_id | |
left join categoria c3 on c3.id = c2.categoria_id | |
left join categoria c4 on c4.id = c3.categoria_id | |
left join categoria c5 on c5.id = c4.categoria_id | |
left join categoria c6 on c6.id = c5.categoria_id | |
left join categoria c7 on c7.id = c6.categoria_id | |
left join categoria c8 on c8.id = c7.categoria_id | |
left join categoria c9 on c9.id = c8.categoria_id | |
left join categoria c10 on c10.id = c9.categoria_id | |
where c1.id = ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select c1.id as id1, c2.id as id2, c3.id as id3, c4.id as id4, c5.id as id5, c6.id as id6, c7.id as id7, c8.id as id8, c9.id as id9, c10.id as id10 | |
from categoria c1 | |
left join categoria c2 on c2.categoria_id = c1.id | |
left join categoria c3 on c3.categoria_id = c2.id | |
left join categoria c4 on c4.categoria_id = c3.id | |
left join categoria c5 on c5.categoria_id = c4.id | |
left join categoria c6 on c6.categoria_id = c5.id | |
left join categoria c7 on c7.categoria_id = c6.id | |
left join categoria c8 on c8.categoria_id = c7.id | |
left join categoria c9 on c9.categoria_id = c8.id | |
left join categoria c10 on c10.categoria_id = c9.id | |
where c1.id = ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with recursive ascendientes as ( | |
select id as id | |
from Categoria c | |
where id = ? | |
union all | |
select c.id as id | |
from ascendientes | |
join categoria c on ascendientes.id = c.parent) | |
select id from ascendientes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with recursive descendientes as ( | |
select id as id | |
from categoria c | |
where id = ? | |
union all | |
select c.id as id | |
from descendientes | |
join categoria c on c.parent = descendientes.id) | |
select id from descencientes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice post here thanks for it I always like and search such topics and everything connected to them.Excellent and very cool idea and the subject at the top of magnificence and I am happy to comment on this topic through which we address the idea of positive re like this.
school branding uk