in reply to DBI, MAP Help!

First, please read How (Not) To Ask A Question, in particular the section on titles.

This is not a Perl issue, but an SQL issue. You can resolve this by using the IN operator.

As a side note, you should seriously consider using placeholders in your code. It will handle any character escaping that has to be performed and adds a layer of security. As an example, here is how you might form your $sql variable for finding all occurrences in your year array:

my $sql = "SELECT DISTINCT name, email, addr, location FROM my_users WHERE year IN (" . join(', ', ('?') x @year) +. ") "; $i = 0; my $query = $dbh->prepare($dbh) or die "Prepare failure: ".$dbh->errst +r; foreach my $year (@year) { $query->bind_param(++$i,$year); } $query->execute or die "Execute failure: ".$dbh->errstr;

Replies are listed 'Best First'.
Re^2: DBI, MAP Help!
by runrig (Abbot) on Apr 30, 2009 at 20:28 UTC
    For IN clauses (especially that many IN clauses), if there are variable numbers of parameters, and especially if the statement (with the fixed number of parameters) is not reused, I wouldn't bother with placeholders. The DBI quote() method is fine.

    And I disagree about the !Perl but SQL issue. It was a Perl/SQL issue IMHO.

      Yeah, my language is a little strong. I agree that quote is fine for a single use query, but I think that in the best case scenarios they tie and in worst cases placeholders fare dramatically better, so they should be in a programmer's arsenal. I also think that performing the N^M queries necessary to do this as a series of one-offs is unnecessary abuse of a database when 1 straight forward query will do.

      In any case, TIMTOWTDI and best practice can mean ignoring Best Practice.