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 );