My original point is that a statement like this:
select a , b , c from test
where (a = :var1 and b = :var2)
or (a = :var1 and c = :var3)
is equivalent to a statement like this:
select a , b , c from test
where a = :var1 and ( b = :var2 or c = :var3 )
The difference isn't a matter of Perl experience; it's just
that the latter query is simpler, easier to
maintain, quicker to type, and maybe even faster on
execution (if your DBMS isn't very good at optimizing
queries). And you don't have to worry about whether you
need to repeat a bind_param statement for a repeated
value.
I honestly don't know whether you need to repeat bind_param
when using the same variable more than once in a statment,
because I use the simplest form of statement I can, always.
Anyway, I think the other answers about using the "?" place
holder for bindings will be useful for you; note the following
examples, which should both work, and yield the same result:
$sth = $dbh->prepare("select a from b where (c=? and d=?) or (c=? and
+f=?)");
$sth->execute( $cval, $dval, $cval, $fval );
or, more optimally:
$sth = $dbh->prepare("select a from b where c=? and (d=? or f=?)");
$sth->execute( $cval, $dval, $fval );
(update fixed the prepare calls in the
closing examples.) |