Pregunta

I have written the following SQL query:

$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);

However I can't seem to get the syntax for the named parameter in the 'NOT IN' clause correct as I get the following error:

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 

Really appreciate it if someone can point me in the right direction.

¿Fue útil?

Solución

Remove additional WHERE after AND operator

AND WHERE query.media_category_id  
    ^here 

Otros consejos

Use following query, no another WHERE needed:

SELECT `id`
    FROM query
    WHERE 1=1
    AND query.media_category_id NOT IN (:media_category_ids)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top