in reply to in, ands, ors in query

I hope you've gotten the syntax fixed by now so that it's working -- the replies above are clear enough about what the problem is. Once you have it working, you might think about some "stylistic" improvements:
my @getfields = qw/engine/; for my $type ( qw/name lable settings/ ) { for my $num ( 1 .. 4 ) { push @getfields, "$type$num"; } } my $sql = 'select '. join( ',', @getfields ). ' from special_fields WHERE engine IN ('. join( ',', ('?') x scalar @choices ). ') AND ('. join( ' OR ', map { "settings$_ = 'public'" } 1..4 ). ')'; my $sth = $dbh->prepare( $sql ); $sth->execute( @choices );
Notes: (update: having seen jdporter's reply, I have to agree -- his version is much easier to read)

Replies are listed 'Best First'.
Re^2: in, ands, ors in query
by jdporter (Paladin) on Dec 13, 2006 at 16:50 UTC

    I personally find that style of sql construction hard to read. How about this:

    my $sql = do { my $getfields = join ',', @getfields; my $placeholders = join ',', ('?') x @choices; my $settingspublic = join ' OR ', map "settings$_ = 'public'", 1 .. +4; <<EOF; select $getfields from special_fields where engine in ($placeholders) and ($settingspublic) EOF };
    We're building the house of the future together.