如何在MySQL查询中为NOT IN子句指定命名参数值?

时间:2014-02-13 15:09:07

标签: mysql doctrine-orm prepared-statement named-parameters notin

我编写了以下SQL查询:

$media_category_ids = array( 11, 12);
$params = array();
$sql = "SELECT `id`
    FROM query
    WHERE 1=1
    AND WHERE query.media_category_id NOT IN (:media_category_ids)";
$params['media_category_ids'] = implode(",",$media_category_ids);
$prepared_query = $c->prepare($sql);
$prepared_query->execute($params);

但是我似乎无法在'NOT IN'子句中获得指定参数的语法,因为我收到以下错误:

Message: An exception occurred while executing 'SELECT `id` FROM query WHERE 1=1 AND WHERE query.media_category_id NOT IN (:media_category_ids)': SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE query.media_category_id NOT IN ('11,2')' at line 4 

如果有人能指出我正确的方向,我真的很感激。

2 个答案:

答案 0 :(得分:1)

WHERE运算符

之后删除其他AND
AND WHERE query.media_category_id  
    ^here 

答案 1 :(得分:0)

使用以下查询,不需要其他WHERE

SELECT `id`
    FROM query
    WHERE 1=1
    AND query.media_category_id NOT IN (:media_category_ids)