Solved

Amendment to MYSQL code for parent / child entries

Posted on 2010-08-27
9
378 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
  • 5
  • 4
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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 142

Expert Comment

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

Author Comment

by:mvwmail
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 3

Author Comment

by:mvwmail
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
Excellent, thank you
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now