Amendment to MYSQL code for parent / child entries

Attached is an amazing piece of code written by andyc75 several weeks ago

It can basically take a table of objects, that can be children of each other and sort it into levels. It can do three deep at the moment, but I would like to add a fourth level if possible. I have tried to work through the code and add a fourth level myself, but it is completely beyond this novice coder and I need some assistance.

Here is an example table to work with

CREATE TABLE IF NOT EXISTS `zones` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zone` varchar(10) NOT NULL,
  `parent` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

INSERT INTO `zones` (`id`, `zone`, `parent`) VALUES
(1, 'Mit', 0),
(2, 'Tae', 0),
(3, 'Ask', 1),
(4, 'Sew', 3),
(5, 'Des', 2),
(6, 'Cav', 5),
(7, 'Kea', 0),
(8, 'Tro', 7);
(9, 'Bat', 4);

The layout, in a nice easy viewable format should be

The layout should be (indented for easy viewing)

Kea
  - Tro
Mit
 - Ask
   - Sew
     -Bat
Tae
 - Des
   - Cav

Select 	a.id as a_id,
	a.zone as a_zone,
	a.parent as a_parent,
	coalesce((CASE a.parent WHEN 0 THEN 1 ELSE null END),
			 (CASE b.parent WHEN 0 THEN 2 ELSE 3 END)
			) as level,
	coalesce((CASE a.parent WHEN 0 THEN a.id  ELSE null END),
			 (CASE b.parent WHEN 0 THEN a.parent ELSE b.parent END)
			)as sort_value,
	coalesce((CASE a.parent WHEN 0 THEN a.zone  ELSE null END),
			 (CASE b.parent WHEN 0 THEN b.zone ELSE c.zone END)
			)as sort_desc
from zones a
    inner join zones b on (CASE a.parent WHEN 0 THEN a.id ELSE a.parent END) = b.id
	left outer join zones c on b.parent > 0 and b.parent = c.id 
order by  sort_desc, level, a.id, b.id

Open in new window

LVL 3
mvwmailAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
I think you should read this page to manage hierarchies properly:
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
0
mvwmailAuthor Commented:
Have read it several weeks ago thanks. The amount of data I am handling is quite small, so if the server has to do a little extra work to calculate the table, it will be barely noticable.

The code that was submitted works perfectly, hence why I am am hoping to expand on it. If I have to rework the entire dbase at a later time, I will, for now, why break something that works like a charm.

This is for a small fun project only, and I am using my paid subscription to EE to be lazy, not to be educated, but I appreciate the speed of the reply and the useful (if not new) link :-)
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
so, why not simply add the 4th level into the query with another join
Select  a.id as a_id,
        a.zone as a_zone,
        a.parent as a_parent,
        coalesce((CASE a.parent WHEN 0 THEN 1 ELSE null END),
                         (CASE b.parent WHEN 0 THEN 2 ELSE 3 END)
                        ) as level,
        coalesce((CASE a.parent WHEN 0 THEN a.id  ELSE null END),
                         (CASE b.parent WHEN 0 THEN a.parent ELSE b.parent END)
                        )as sort_value,
        coalesce((CASE a.parent WHEN 0 THEN a.zone  ELSE null END),
                         (CASE b.parent WHEN 0 THEN b.zone ELSE c.zone END)
                        )as sort_desc
from zones a
inner join zones b on (CASE a.parent WHEN 0 THEN a.id ELSE a.parent END) = b.id
left outer join zones c on b.parent > 0 and b.parent = c.id 
left outer join zones d on c.parent > 0 and d.parent = c.id 
order by  sort_desc, level, a.id, b.id, c.id, d.id

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

mvwmailAuthor Commented:
Having re-read that, it comes across as potentially unfriendly, this is not intended and appologies if it appears that way. I would edit it, but cant see an edit button!

The code included actually generates an immediatly useable list that I can use in a table, and use the LEVEL variable for a css class so I can have a nice indented table. The screen shot shows the finished product, and how easy it is to generatet the CSS code which indents the levels making it easily viewable.

The only model I can use ftom the article is the nested model which generates the same results, but would require me to rework a lot of code for editting / updating / deleting.
screenshot.jpg
0
mvwmailAuthor Commented:
The fourth level item (BAT) with your SQL code still shows as level 3, and at the top of the table. Appreciate the attempt though
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I missed something, I presume :)

select l4.id, l4.zone,
case when l4.id = l1.id then 1
       when l3.id = l1.id then 2
       when l2.id = l1.id then 3
       else 4 end level
from zones l1
join zones l2
  on l2.parent = l1.id
  or l2.id = l1.id
join zones l3
  on l3.parent = l2.id
  or l3.id = l2.id
  or l3.id = l1.id
join zones l4
  on l4.parent = l3.id
  or l4.id = l3.id
  or l4.id = l2.id
  or l4.id = l1.id
where l1.parent = 0
   and not ( l4.id <> l3.id and l4.id in ( l1.id, l2.id ) )
   and not ( l4.id = l3.id and l4.id <> l2.id and l2.id = l1.id  )
   and not ( l4.id = l3.id and l4.id = l2.id and l4.id <> l1.id  )
   and not ( l4.id = l3.id and l4.id <> l2.id )
   and not ( l4.id = l3.id and l4.id <> l2.id )
   and not ( l3.id = l2.id and l3.id <> l1.id )

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mvwmailAuthor Commented:
Well that code certainly gets the level right, so thats a plus! However, it gets the order completely wrong.

With this table

CREATE TABLE IF NOT EXISTS `zones` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zone` varchar(10) NOT NULL,
  `parent` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `zones`
--

INSERT INTO `zones` (`id`, `zone`, `parent`) VALUES
(1, 'Mit', 0),
(2, 'Tae', 0),
(3, 'Ask', 1),
(4, 'Sew', 3),
(5, 'Des', 2),
(6, 'Cav', 5),
(7, 'Kea', 0),
(8, 'Tro', 7),
(10, 'Bat', 4);


The output is

id  zone  level  
1 Mit 1
3 Ask 2
4 Sew 3
2 Tae 1
5 Des 2
6 Cav 3
10 Bat 4
7 Kea 1
8 Tro 2

As you can see, BAT is certainly the right level (4) however its parent is 4 Sew, yet it is appearing under 6 Cav
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you please try to add this at the end of the query:
order by concat(l1.id, '|' , l2.id, '|', l3.id, '|', l4.id )

Open in new window

0
mvwmailAuthor Commented:
Excellent, thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.