1

Is it possible to achieve something like this in sql?

ID!= {2,3}

where ID is a column. Or I have to use multiple OR statements?

gdoron
  • 147,333
  • 58
  • 291
  • 367
OBL
  • 1,347
  • 10
  • 24
  • 45

2 Answers2

5

yes, not in:

ID not in (2,3)

You can read more here.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • or `not ID in (2,3)` - I've had problems with the above syntax in the past - especially when dealing with NULL values. – Matt Tew Apr 26 '12 at 21:07
  • @MattTew. NULL values is DB are "nothing" so you need to be careful with those. And different `DBMS`s can handle it other way. But except that, what kind of problems did you have with `not in`? – gdoron Apr 26 '12 at 21:09
  • I can't say off hand. I just know that I have scratched my head in the past wondering why NOT IN wasn't working, but reversing the syntax worked (using MySQL). I think I was also using a subquery to get the set. Your answer is correct (and I upvoted it), just adding my 2c. – Matt Tew Apr 26 '12 at 21:13
  • @MattTew. ohh subqueries, Now I get it. You should use `EXIST` or `NOT EXIST` with NULL subqueries. – gdoron Apr 26 '12 at 21:15
3

You have to try With

ID NOT IN (2,3)

Or

ID <> 2 or ID <> 3
gdoron
  • 147,333
  • 58
  • 291
  • 367
Vimal bhatt
  • 285
  • 1
  • 2
  • 7
  • You probably meant: `id <> 2 **AND** id <> 3`. Your condition will always be true.. id can't be equals to 2 **and** equals to 3 at the same time! – gdoron May 01 '12 at 21:37