我有几个条件要在单个查询中完成。基本查询:
$query = "select * from table1
inner join
table2 on table1.id=table2.id
where
".$condition."
limit 0,100;";
我有要查询的值数组:
$Var = array(
1 => array('name' => 'somename1','value' => 'somevalue1'),
2 => array('name' => 'somename2','value' => 'somevalue2'),
3 => array('name' => 'somename3','value' => 'somevalue3'),
4 => array('name' => 'somename4','value' => 'somevalue4')
)
现在在循环中
foreach($Var as $Array){
$condition .= '(T1_column1 = '.$Array['name'].' and T2_column1 = '.$Array['value'].') or ';
}
最后结果:
$query = "select * from table1
inner join
table2 on table1.id=table2.id
where
(T1_column1 = somename1 and T2_column1 = somevalue1) or
(T1_column1 = somename2 and T2_column1 = somevalue2) or
(T1_column1 = somename3 and T2_column1 = somevalue3) or
(T1_column1 = somename4 and T2_column1 = somevalue4)
limit 0,100;";
有没有办法使用 PDO 做同样的事情:prepare,bindvalue,execute ?或 foreach 循环中的 bindValue 具有相同的效果?我怎样才能做到这一点?