finding rows that contain a set but not anything else....
Posted in 1997
Topics: General Discussion
I have a table with 2 columns: name , number. I want to get all numbers
where name equals X or Y but not anything else. If i do a simple "where
name=X or name=Y" it will bring back numbers that could also have another
name. Here is sample data to illustrate this:
name number
X 1
X 2
Y 2
Y 1
Z 1
For the data above i just want to return "2". I don't want 1 because there
Z has it as a number. Here is the query i came up with.
SELECT number FROM table
WHERE name = X or name=Y ANDnumber NOT IN
( SELECT number FROM table
WHERE name <> X AND name <> Y)
This works, but this table is going to get to be as large as 50k rows and
this would really tax the server. In addition the query is going to be much
more complex. Can't i create a truth table or something and write functions
to insert/update/delete the table? Is this standard practice, or is there a
better way to do this?