Anonjg8934
Anonjg8934

Reputation: 59

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!

Edit: Here is the complete query that gets the correct values:

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

Upvotes: 1

Views: 16788

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270463

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

Upvotes: 2

Related Questions