0

可以说我有 1 个表只有多列。让我们说:

Fruits
--------------------------------------
id  | name        | country
--------------------------------------
1   | Banana      | china
2   | Orange      | japan
3   | Apple       | japan
4   | Apple       | china
5   | Banana      | usa
6   | Orange      | china

然后对SELECT所有 Fruits where进行简单查询id < 6
这是:

SELECT * FROM fruits WHERE id < 6

然后它将返回:

--------------------------------------
id  | name        | country
--------------------------------------
1   | Banana      | china
2   | Orange      | japan
3   | Apple       | japan
4   | Apple       | china
5   | Banana      | usa

然后通过附加上述查询,我​​怎样才能继续排除更多内容。
例如:

Do NOT want to see, any "APPLE" from "CHINA".

所以最终的结果一定是:

--------------------------------------
id  | name        | country
--------------------------------------
1   | Banana      | china
2   | Orange      | japan
3   | Apple       | japan
5   | Banana      | usa

这又是TOGETHERwithout的行了!if, name='Apple' AND country='china'.

  • 那么通过继续第一个查询,我怎样才能添加附加的“ AND 条件对”来获得这个结果呢?

我会说:

SELECT * FROM fruits WHERE id < 6 ... AND DON'T RETURN this condition where name='Apple' and country='china' TOGETHER
4

2 回答 2

2

尝试:

SELECT * 
FROM fruits 
WHERE (id < 6)
AND NOT (name = 'Apple' AND country = 'china')

或者:

SELECT * 
FROM fruits 
WHERE (id < 6)
AND id NOT IN (SELECT id 
               FROM fruits
               WHERE name = 'Apple'
               AND country = 'china')
于 2013-04-22T11:02:42.723 回答
1

尝试这个

 SELECT * FROM fruits WHERE 
                 (name ,country) not in ( select 'Apple' ,'china' from Fruits)

 HAVING id < 6

在这里演示

于 2013-04-22T11:02:30.580 回答