PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > ORDER BY with hierarchical data
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
ORDER BY with hierarchical data

Réponse
 
LinkBack Outils de la discussion
Vieux 01/02/2008, 14h28   #1
pieter.thoma@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ORDER BY with hierarchical data

Hi,

I have a question regarding sorting and ordering of hierarchical data
following this tutorial: http://dev.mysql.com/tech-resources/...ical-data.html.

When using the Modified Pre-order Tree Traversal algorithm, is there a
way to have MySQL sort the names, but retaining the tree order?

When looking at this query:

SELECT CONCAT( REPEAT(' ', COUNT(parent.name) - 1), node.name) AS name
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;

Everything is ordered by node.lft, which contains unique values along
the whole column. This means adding another ordering by node.name will
have no effect.

Additionally, When putting everything into a result table to order on
that, we loose the relationship between the nodes.
SELECT * FROM (
SELECT CONCAT( REPEAT(' ', COUNT(parent.name) - 1), node.name) AS name
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft) AS t3
ORDER BY t3.name

Is there a way out of this problem?

Thanks


  Réponse avec citation
Vieux 01/02/2008, 16h55   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ORDER BY with hierarchical data

On Fri, 01 Feb 2008 15:28:37 +0100, <pieter.thoma@gmail.com> wrote:

> Hi,
>
> I have a question regarding sorting and ordering of hierarchical data
> following this tutorial:
> http://dev.mysql.com/tech-resources/...ical-data.html.
>
> When using the Modified Pre-order Tree Traversal algorithm, is there a
> way to have MySQL sort the names, but retaining the tree order?
>
> When looking at this query:
>
> SELECT CONCAT( REPEAT(' ', COUNT(parent.name) - 1), node.name) AS name
> FROM nested_category AS node,
> nested_category AS parent
> WHERE node.lft BETWEEN parent.lft AND parent.rgt
> GROUP BY node.name
> ORDER BY node.lft;
>
> Everything is ordered by node.lft, which contains unique values along
> the whole column. This means adding another ordering by node.name will
> have no effect.
>
> Additionally, When putting everything into a result table to order on
> that, we loose the relationship between the nodes.
> SELECT * FROM (
> SELECT CONCAT( REPEAT(' ', COUNT(parent.name) - 1), node.name) AS name
> FROM nested_category AS node,
> nested_category AS parent
> WHERE node.lft BETWEEN parent.lft AND parent.rgt
> GROUP BY node.name
> ORDER BY node.lft) AS t3
> ORDER BY t3.name
>
> Is there a way out of this problem?


What is it exactly that you want? Siblings ordered by name, with tree
'retention'? This could be a hack that does it, not 100% reliable though
(I like explicit joins, they are more clear to me):

SELECT CONCAT( REPEAT(' ', COUNT(parent.name)-1), node.name) AS name,
GROUP_CONCAT(parent.name ORDER BY parent.lft SEPARATOR '~') as 'path',
node.lft
FROM nested_category AS node
LEFT JOIN nested_category AS parent
ON node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY path,node.name;

'~' is chosen because of it's high ascii value. In case of unicode /
higher characters, this could potentially break. If the
possibilities/character set for node.name is limited, this will work
though.
+-----------------------+----------------------------------------------------+-----+
| name |
path | lft |
+-----------------------+----------------------------------------------------+-----+
| ELECTRONICS |
ELECTRONICS | 1 |
| PORTABLE ELECTRONICS | ELECTRONICS~PORTABLE
ELECTRONICS | 10 |
| 2 WAY RADIOS | ELECTRONICS~PORTABLE ELECTRONICS~2 WAY
RADIOS | 17 |
| CD PLAYERS | ELECTRONICS~PORTABLE ELECTRONICS~CD
PLAYERS | 15 |
| MP3 PLAYERS | ELECTRONICS~PORTABLE ELECTRONICS~MP3
PLAYERS | 11 |
| FLASH | ELECTRONICS~PORTABLE ELECTRONICS~MP3
PLAYERS~FLASH | 12 |
| TELEVISIONS |
ELECTRONICS~TELEVISIONS | 2 |
| LCD |
ELECTRONICS~TELEVISIONS~LCD | 5 |
| PLASMA |
ELECTRONICS~TELEVISIONS~PLASMA | 7 |
| TUBE |
ELECTRONICS~TELEVISIONS~TUBE | 3 |
+-----------------------+----------------------------------------------------+-----+
--
Rik Wasmus
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h24.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10039 seconds with 10 queries