PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Logiciels d'hébergement > mailing.database.mysql > Selecting/Ordering by prerequisite?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Selecting/Ordering by prerequisite?

Réponse
 
LinkBack Outils de la discussion
Vieux 19/06/2006, 00h01   #1
AngleWyrm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Selecting/Ordering by prerequisite?

So I have a table that has, among other things, these feilds:

id PRIMARY KEY
name VARCHAR(50)
prerequisite INT NOT NULL

Prerequisite contains a reference to the id of whatever item comes before
it.

How can I generate a query on this that will put them in sequence?


  Réponse avec citation
Vieux 20/06/2006, 01h06   #2
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selecting/Ordering by prerequisite?

AngleWyrm wrote:
> Prerequisite contains a reference to the id of whatever item comes before it.
> How can I generate a query on this that will put them in sequence?


You haven't described what sequence you want them to be in.

Basically, you're describing tree-structured or heirarchical data, like
a parts-explosion, or threaded forum postings. You can show this
depth-first, breadth-first, or some other sequence.

Regards,
Bill K.
  Réponse avec citation
Vieux 21/06/2006, 10h55   #3
AngleWyrm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selecting/Ordering by prerequisite?

"Bill Karwin" <bill@karwin.com> wrote in message
news:e77akl01vog@enews3.newsguy.com...
> AngleWyrm wrote:
>> Prerequisite contains a reference to the id of whatever item comes before
>> it.
>> How can I generate a query on this that will put them in sequence?

>
> You haven't described what sequence you want them to be in.


An example set of records:

ID | Name | Prerequisite
1 |One |
2 |Two |1
3 |Three |2

In the example above, Three comes after Two, because it has a prerequisite
of item ID 2.

This gives a basic feel for the problem. And no, the ID isn't such an neatly
arranged numerical sequence.


  Réponse avec citation
Vieux 22/06/2006, 22h04   #4
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selecting/Ordering by prerequisite?

AngleWyrm wrote:
> ID | Name | Prerequisite
> 1 |One |
> 2 |Two |1
> 3 |Three |2
>
> In the example above, Three comes after Two, because it has a prerequisite
> of item ID 2.


But what about this case:

ID | Name | Prerequisite
1 |One |
2 |Two |1
3 |Three |2
4 |Four |1

What should the sequence be? "One, Two, Three, Four," or "One, Two,
Four, Three"? That's the difference between depth-first and
breadth-first, respectively.

Breadth-first is pretty easy:

SELECT *
FROM this_table
ORDER BY Prerequisite, ID

Depth-first is harder. You probably need to store the relationships
differently. You could implement the heirarchy using the nested-set
data model.
See http://dev.mysql.com/tech-resources/...ical-data.html

Regards,
Bill K.
  Réponse avec citation
Vieux 23/06/2006, 02h21   #5
AngleWyrm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selecting/Ordering by prerequisite?

"Bill Karwin" <bill@karwin.com> wrote in message
news:e7et4a0n74@enews4.newsguy.com...
> AngleWyrm wrote:
>> ID | Name | Prerequisite
>> 1 |One |
>> 2 |Two |1
>> 3 |Three |2
>>
>> In the example above, Three comes after Two, because it has a
>> prerequisite of item ID 2.

>
> But what about this case:
>
> ID | Name | Prerequisite
> 1 |One |
> 2 |Two |1
> 3 |Three |2
> 4 |Four |1
>
> What should the sequence be? "One, Two, Three, Four," or "One, Two, Four,
> Three"? That's the difference between depth-first and breadth-first,
> respectively.
>
> Breadth-first is pretty easy:
>
> SELECT *
> FROM this_table
> ORDER BY Prerequisite, ID
>
> Depth-first is harder. You probably need to store the relationships
> differently. You could implement the heirarchy using the nested-set data
> model.
> See http://dev.mysql.com/tech-resources/...ical-data.html


There are no cyclical dependencies in the database that I am working with. I
do with it were up to me to alter the structure of the tables to use the
nested-set data model; thank you for the excellent pointer.

Perhaps I can automatically import the data into a private table, and
develop a left and right field for them in the process.


  Réponse avec citation
Vieux 23/06/2006, 04h13   #6
Bill Karwin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selecting/Ordering by prerequisite?

AngleWyrm wrote:
> There are no cyclical dependencies in the database that I am working with.


I know what you mean, but for what it's worth, a tree is acyclic by
definition.

Regards,
Bill K.
  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 11h03.


É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,10018 seconds with 14 queries