?
Solved

Amendment to MYSQL code for parent / child entries

Posted on 2010-08-27
9
Medium Priority
?
425 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:mvwmail
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33540023
I think you should read this page to manage hierarchies properly:
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
0
 
LVL 3

Author Comment

by:mvwmail
ID: 33540315
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33540329
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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
LVL 3

Author Comment

by:mvwmail
ID: 33540394
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
 
LVL 3

Author Comment

by:mvwmail
ID: 33540546
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 33540781
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
 
LVL 3

Author Comment

by:mvwmail
ID: 33541357
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33541395
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
 
LVL 3

Author Closing Comment

by:mvwmail
ID: 33595349
Excellent, thank you
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question