<?xml version="1.0" encoding="windows-1252"?>
<node id="145744" title="Re: Keeping Order with mySQL (Mildly OT)" created="2002-02-15 13:19:32" updated="2005-07-21 01:30:50">
<type id="11">
note</type>
<author id="53423">
Masem</author>
<data>
<field name="doctext">
If you don't mind a few extra SQL calls, you can do a pseudo linked-list; that is, have each element have a 'previous' entry that points to the unique ID of the next element in the list, or NULL if this is the first entry.  Then you can do a loop over a SQL statement such as:
&lt;CODE&gt;
my @order;
my $sth_1 = $dbh-&gt;prepare( "SELECT id FROM TABLE WHERE previous IS NULL" ) or die $DBI-&gt;errstr;
my $sth_2 = $dbh-&gt;prepare( "SELECT id FROM TABLE WHERE previous IS ?" ) or die $DBI-&gt;errstr;
$sth_1-&gt;execute() or die $DBI-&gt;errstr;
if ( $sth_1-&gt;rows() &gt; 0 ) {
    my ($id) = $sth_1-&gt;fetchrow_array();
    push @order, $id;
    $sth_2-&gt;execute( $id ) or die $DBI-&gt;errstr;
    while ( $sth_2-&gt;rows() &gt; 0 ) {
       my ( $id ) = $sth_2-&gt;fetchrow_array();
       push @order, $id;
       $sth_2-&gt;execute( $id ) or die $DBI-&gt;errstr;
    }
}
&lt;/CODE&gt;
(That code could probably be optimized for logic of course.)  
&lt;P&gt;
-----------------------------------------------------
&lt;BR&gt;
&lt;I&gt;
&lt;A HREF="http://mneylon.masemware.com/"&gt;Dr. Michael K. Neylon&lt;/A&gt; - &lt;a href="mailto:mneylon-pm@masemware.com"&gt;mneylon-pm@masemware.com&lt;/a&gt;
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
&lt;BR&gt;
&lt;FONT COLOR="#800000"&gt;&lt;I&gt;"I can see my house from here!"&lt;/I&gt;&lt;/FONT&gt;
&lt;BR&gt;
&lt;FONT COLOR="#808080"&gt;&lt;I&gt;It's not what you know, but knowing how to find it if you don't know that's important&lt;/I&gt;&lt;/FONT&gt;
&lt;/I&gt;</field>
<field name="root_node">
145733</field>
<field name="parent_node">
145733</field>
</data>
</node>
