what's the best technique for selecting records with long comparison in where clause
Say, I need to select records from a table and exclude records with id 1,2,5,9,15. I do th开发者_开发技巧is:
"SELECT * FROM TABLE_NAME WHERE id <> 1 OR id <> 2 OR id <> 5 OR id <> 9 OR id <> 15"
But what if I have like 1000 records and I need to exclude 200 records? Would I have to type 200 " OR id <> id_number"? Or is there a better way to do the query?
Try:
SELECT * FROM TABLE_NAME WHERE id NOT IN (1, 2, 5, 9, 15)
You can maybe exclude a range. Instead of id<>1 OR id<>2 .. id<>5 you can do : id<1 AND id>5. and you can check the "where id not in (1, 2, 3 ...)" option
精彩评论