SQL ...
create table test_members ( username varchar(20), referral varchar(20)); insert into test_members values ('Jack',null); insert into test_members values ('Bob','Jack'); insert into test_members values ('Joe','Bob'); insert into test_members values ('Henry','Joe'); insert into test_members values ('Gabe','Jack'); insert into test_members values ('Pete','Gabe'); insert into test_members values ('Tim','Gabe');
Perl...
use strict; use DBI; my $database='db'; my $username='user'; my $password='pass'; my $dbh=DBI->connect('DBI:mysql:database='.$database, $username,$password); print 'dbh error' if (!$dbh); my @ref = (); my @names = ('Jack'); my $base_sql=q{SELECT username FROM test_members WHERE referral }; while (scalar(@names)) { my $sql=$base_sql.($#names?'in ('.join(',',map('?',@names)).')':'=?' +); print "$sql\n"; my $sth=$dbh->prepare($sql); print 'sth error' if (!$sth); my $rv=$sth->execute(@names); print 'rv error' if (!$rv); my $res = $sth->fetchall_arrayref(); @names=map($res->[$_][0],0..$#$res); push @ref, join '|',@names if (scalar(@names)); } print join(',',@ref),"\n"; print "finished\n";
Should do the job

Hope it helps
UnderMine

Update: username and referral were swaped around.. think referral should be called referrer


In reply to Re: Building a List by UnderMine
in thread Building a List by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.