Solved

Amendment to MYSQL code for parent / child entries

Posted on 2010-08-27
9
419 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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