Reputation: 4555
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?
Upvotes: 3
Views: 976
Reputation: 57907
SELECT * FROM table WHERE
((field = value) AND
(field2 <> value2) AND
(field3 <> value3))
If you're dealing with NULL
, you have to do two things:
SET ANSI_NULLS ON
NULL
values to a dummy value. SQL Cannot compare nulls.
To do that:
SET @value = ISNULL(@value, -1);
Upvotes: 6
Reputation: 44308
have you tried the <>
operator
SELECT * FROM table WHERE field = value AND field2 <> value2
Upvotes: 4
Reputation: 83051
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'
.
Upvotes: 4