因此,我有一个使用“IN”子句的动态构建查询,但实际上需要为两个参数选择,而且似乎无法确定如何执行此操作。

$selectSql= "SELECT groupid FROM groups WHERE name IN(";

foreach ($groupsArray as $group => $row){

  $selectSql .= "'".$row['name']."'".",";

}
$selectSql = rtrim($selectSql, ',');
$selectSql .= ")";

实际上,查询需要类似于:
$selectSql= "SELECT groupid FROM groups WHERE name and type IN(;

我该怎么做呢?
衷心感谢您的帮助!非常感谢。

最佳答案

尝试implode()

foreach ($groupsArray as $group => $row ){
  $name[]= "'".$row['name']."'";
  $type[] = "'".$row['type']."'";
}
$selectSql= "SELECT groupid FROM groups WHERE name IN(".implode(',',$name).") and type IN(".implode(',',$type).")";

关于php - 多个参数进入sql select'in'语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28295522/

10-11 08:07