带有IN和多个条件的原始查询?

时间:2019-12-23 06:49:00

标签: php symfony4

我需要编写一个具有IN条件和多个WHERE条件的查询。我已经尝试过以下SQL语句,但是这一条没有得到我的结果。也许我做错了。

SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters

$columns = "DISTINCT model, manufacturer, logo, year, update_status";

$modelList = array('Version', 'Facelift', 'Optionsänderung');


$orderBy = "manufacturer, model";

// Prepare the sql statement.
$sql = "select $columns from tbl_manufacture where model IN (:modelList)  year > ? AND update_status = ?";
$sql.="order by $orderBy";

$em = $this->getEntityManager();
$query = $em->getConnection()
            ->prepare($sql);
$query->bindValue("modelList", $modelList, Connection::PARAM_STR_ARRAY);

$params = array(
    "year" => $year,
    "update_status" => $status
);

// Execute the statement
$query->execute($params);

2 个答案:

答案 0 :(得分:2)

IN条件后,您缺少“ AND”:

$sql = "select $columns from tbl_manufacture where model IN (:modelList) AND year > ? AND update_status = ?";
$sql.="order by $orderBy";

答案 1 :(得分:1)

您需要将数组转换成逗号分隔的字符串,以便在IN查询中使用它,如下所示:

$modelList = array('Version', 'Facelift', 'Model');
$modelList = "'" . implode ( "', '", $modelList ) . "'";

结果: “版本”,“整容”,“模型”

AND之后您还缺少IN (:modelList)