如何从mysql表中选择值不是......?

时间:2017-11-28 13:26:06

标签: mysql where

输入:

$result = $db->query('SELECT * 
         FROM data 
         ')->fetchAll(PDO::FETCH_ASSOC);

结果:

id    name   status
1     dog    ok
2     frog

输入:

 $result = $db->query('SELECT * 
             FROM data 
             WHERE status = `ok`
             ')->fetchAll(PDO::FETCH_ASSOC);

结果:

id    name   status
1     dog    ok

输入:

 $result = $db->query('SELECT * 
             FROM data 
             WHERE status != `ok`
             ')->fetchAll(PDO::FETCH_ASSOC);

结果:

id    name   status

输入:

 $result = $db->query('SELECT * 
             FROM data 
             WHERE status <> `ok`
             ')->fetchAll(PDO::FETCH_ASSOC);

结果:

id    name   status

我希望得到的结果是WHERE状态不是ok

id    name   status
2     frog

3 个答案:

答案 0 :(得分:1)

试试这个......

$result = $db->query("SELECT * FROM data WHERE status <> 'ok' ")->fetchAll(PDO::FETCH_ASSOC); 

或者如果您要查找空状态的行,请尝试...

$result = $db->query("SELECT * FROM data WHERE status = '' ")->fetchAll(PDO::FETCH_ASSOC); 

如果您想使用单引号,请使用以下内容:

$result = $db->query('SELECT * FROM data WHERE status <> \'ok\' ')->fetchAll(PDO::FETCH_ASSOC); 

注意引号用斜杠转义。

答案 1 :(得分:0)

SELECT * FROM data WHERE status NOT LIKE 'ok';

或该字段是否为Nullable

SELECT * FROM data WHERE status IS NULL;

答案 2 :(得分:0)

尝试一下,这更像是你的要求:

$result = $db->query('SELECT * 
    FROM `data` 
    WHERE NOT `status` = "ok"
    ')->fetchAll(PDO::FETCH_ASSOC);