Double WHERE NOT语句sql

时间:2014-07-23 09:16:53

标签: php mysql sql

我有这个问题:

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

我为所有用户选择pic_squarenamehighest_scorenum_triesavg_total FROM

不要将他们玩过的游戏设置为' 0''


我想添加其他条件,因此我只选择以下用户:

  • 请勿将他们的plays_game设置为' 0''
  • 请勿将其名称清空

PS: 名称不能为空,它只能为空,我无法更改数据库结构本身。

那么如何将这些双重WHERE一起添加?

5 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

$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";

答案 2 :(得分:1)

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

只需改变条件,如

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

答案 3 :(得分:1)

您可以使用此查询:

$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";

使用IFNULL,您可以确保NULL值和空值的处理方式相同。

答案 4 :(得分:1)

$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";