Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Say you have an array of @names and you have these names as columns inside your database.

Without looping over the array and connecting to the database infinitely many times, how can I do it all in one run?

@names = qq(aaron richard robert penny); my $data = qq(SELECT id, order_num, state FROM orders WHERE names=@nam +es); ...
This is for a MySQL connection.

Replies are listed 'Best First'.
Re: confused about reading from database using an array
by Joost (Canon) on Dec 12, 2006 at 20:46 UTC
      Are you saying I can replace the list with the array?
      SELECT id, order_num, state FROM orders WHERE names IN (@names)
Re: confused about reading from database using an array
by sgifford (Prior) on Dec 12, 2006 at 22:30 UTC
    For an extremely large list of names (hundreds), you could also create a temporary table to JOIN against. There's often a length limit for SQL queries, and creating a giant IN list would bump up against that.

    If you've just got a few (around 100 or less), using IN or OR should work fine.