S
S
sitev_ru2015-09-30 10:34:29
MySQL
sitev_ru, 2015-09-30 10:34:29

What query should be written to display the nesting level for a menu item?

The menu table is simple name, url, parent (name, where to go and the parent menu item) ...
What query should be written to display another level - the nesting level for the menu item?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Taratin, 2015-09-30
@Taraflex

nested sets

D
Dmitry Kravchenko, 2015-09-30
@mydearfriend

select * from tbl_menu
then a loop in which a data structure is formed that is convenient for display

A
Andrey Balin, 2015-09-30
@lod2007

Here, catch it. Here the syntax is in T-SQL, but it is easy to remake it for another DBMS. On Habré there is an article about recursion. there and about MySQL the recursion is shown.
go
with myrec (ProdName,id,idParent,levell) as
(
Select CONVERT(varchar(255), ProdName),id,idParent,0 from Products where idParent=-1
union ALL
Select CONVERT(varchar(255),d.ProdName+ '\'+a.ProdName) as ProdName,a.id,a.idParent,levell+1 from Products a
inner join myrec d on d.id=a.idParent)
select b.ProdName as ProdName,b.id,b .idParent,levell from Products a
right join myrec b on a.id = b.idparent
where b.id=(select idparent from Products where id=35)
go

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question