用不同数量的 WHERE 条件构造 sql 的最佳方法是什么?我的解决方案看起来很难看:
my ($where, @values);
if ($phone_number)
{
$where = 'AND pnone_number=?';
@values = ($from, $till, $phone_number);
}
else
{
$where = '';
@values = ($from, $till);
}
my $sql = 'SELECT * FROM calls WHERE time between ? AND ? '.$where.' ORDER BY time';
my $res = $dbh->selectall_arrayref($sql, undef, @values) or warn 'error';