nicholaswmin
nicholaswmin

Reputation: 22969

Double WHERE NOT statement sql

I have this query:

$query = "SELECT pic_square,
                 name,
                 highest_score,
                 num_tries,
                 avg_total 
            FROM users 
       WHERE NOT played_game = '0' 
        ORDER BY avg_total DESC";

Where I select pic_square,name,highest_score,num_tries and avg_total FROM for all users that

DO NOT have their played game set as ''0''.


I would like to add another condition so I select only the users that:

PS: The name cannot be null, it just can be empty, I cannot change the DB structure itself.

So how do I add these dual WHERE NOT together?

Upvotes: 0

Views: 195

Answers (5)

Oscar Pérez
Oscar Pérez

Reputation: 4397

You can use this query:

$query = "SELECT pic_square,
                 name,
                 highest_score,
                 num_tries,
                 avg_total 
            FROM users 
           WHERE NOT played_game = '0' 
             AND IFNULL(name,'') <> ''
        ORDER BY avg_total DESC";

Using IFNULL you'll get sure that NULL values and empty values are treated just the same.

Upvotes: 1

Mahipat Kanzariya
Mahipat Kanzariya

Reputation: 148

$query = "SELECT pic_square,name,highest_score,num_tries,avg_total 
          FROM users 
          WHERE played_game != '0' 
            AND name NOT NULL 
          ORDER BY avg_total DESC";

Upvotes: 1

Sadikhasan
Sadikhasan

Reputation: 18600

$query = "SELECT pic_square,name,highest_score,num_tries,avg_total FROM users 
  WHERE played_game <> '0' AND name <> ''  ORDER BY avg_total DESC";

simply change condition like

where played_game <> '0' AND name <> ''

Upvotes: 1

juergen d
juergen d

Reputation: 204854

WHERE NOT (played_game = '0' or name = '')

or

WHERE played_game <> '0' and name <> ''

Upvotes: 2

Ilesh Patel
Ilesh Patel

Reputation: 2155

$query = "SELECT pic_square,name,highest_score,num_tries,avg_total FROM users 
  WHERE NOT (played_game = '0' OR name IS NULL OR name='' ) 
  ORDER BY avg_total DESC";

Upvotes: 1

Related Questions