Jesper Heikkilä Jesper Heikkilä - 6 months ago 14
SQL Question

SQL Select statement for NOT IN multiple values

Im trying to Select some values from a table and I need to exclude other values.

SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id = 8 AND NOT
IN(SELECT term_taxonomy_id FROM wp_term_relationships WHERE object_id =
1 OR 2 OR 3 OR 4 OR 5)


As you can see I want the term_taxonomy_id from object_id where object_id is not 1-5.

Im not sure what to do here and any help is apreciated!

Answer

Use IN and explicit comparisons:

SELECT term_taxonomy_id
FROM wp_term_relationships
WHERE object_id = 8 AND 
      object_id NOT IN (SELECT term_taxonomy_id
                        FROM wp_term_relationships
                        WHERE object_id IN (1, 2, 3, 4, 5)
                       )
Comments