tags:

views:

228

answers:

4

I can't find the answer since searching mysql NOT in google is a nightmare (even with the quotes).

I need to make a query like this:

SELECT * FROM table WHERE field=value AND field2!=value2 AND field3!=value3

How it is done? Is it even possible?

+4  A: 

Yes, you can do exactly what you wrote, but use <> instead of !=

Perhaps the answer depends on what "value" is? For example, for an integer 123 value would be 123; for a string "foobar" value would be 'foobar'.

Jason Cohen
+2  A: 

have you tried "<>"? it works in Delphi

Peter Turner
+6  A: 
SELECT * FROM table WHERE 
        ((field = value) AND  
        (field2 <> value2) AND 
        (field3 <> value3))

If you're dealing with NULL, you have to do two things:

  1. Use SET ANSI_NULLS ON
  2. Declare NULL values to a dummy value.

SQL Cannot compare nulls.

To do that:

SET @value = ISNULL(@value, -1);
George Stocker
typo there I think... field3 <> field3 will always evaluate to a non-match won't it ;)
Eoin Campbell
fixed it for you
Patrick McDonald
I had copied the Author's original post before he edited it. So that's why that was there.
George Stocker
+4  A: 

have you tried the <> operator

SELECT * FROM table WHERE field = value AND field2 <> value2
Eoin Campbell