在sql中不等于条件

时间:2014-02-20 09:15:18

标签: mysql postgresql

假设我有一张名为students的表。

列是:

student_id
first_name
last_name
address

在输出中,我想排除地址为“地址”或“地址,此处”的学生。

我试过

SELECT * FROM students WHERE address <> 'address' OR address <> 'address, here';

它不起作用。

它只适用于

SELECT * FROM students WHERE address <> 'address';

怎么做?

4 个答案:

答案 0 :(得分:2)

怎么样

SELECT * 
FROM students 
WHERE address NOT IN ('address','address, here')

答案 1 :(得分:1)

使用AND而不是OR

SELECT * FROM students WHERE address <> 'address' AND address <> 'address, here';

address <> 'address' OR address <> 'address, here'将始终为真,因为一个地址不能同时为'address''address, here'

答案 2 :(得分:0)

SELECT * 
FROM students 
WHERE 
    address <> 'address' 
    AND 
    address <> 'address, here'

答案 3 :(得分:0)

试试这个:

SELECT * FROM students WHERE (address <> 'address' AND address <> 'address, here');