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



我编写了以下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 

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

删除运算符

AND的其他WHERE

AND WHERE query.media_category_id  
    ^here 

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

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

相关内容

  • 没有找到相关文章