0plus1
0plus1

Reputation: 4555

SELECT WHERE field!=value how it's done in mysql?

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

Answers (4)

George Stocker
George Stocker

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:

  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);

Upvotes: 6

Eoin Campbell
Eoin Campbell

Reputation: 44308

have you tried the <> operator

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

Upvotes: 4

Peter Turner
Peter Turner

Reputation: 11435

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

Upvotes: 2

Jason Cohen
Jason Cohen

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

Related Questions