看看我的桌子,我正试图从美国获得水果运输以及任何不包括美国的水果。看看我的桌子,苹果包括美国和ups,浆果不包括美国。所以我想得到苹果和浆果。下面的查询是我得到的最好的,但它失败了,它不会回应不包括美国的浆果。
SELECT fruit FROM table fruit WHERE shipping = 'ups' AND country ='USA' GROUP BY fruit
table fruit
fruit country shipping
apple USA ups
apple China ups
apple UK ups
banana USA fedez
banana Russia fedez
banana Italy fedez
berry Italy fedez
berry China fedez
berry French fedez
答案 0 :(得分:2)
SELECT fruit FROM fruit WHERE (country = "USA" AND shipping = "ups") OR fruit NOT IN (SELECT fruit FROM fruit WHERE country = "USA") GROUP BY fruit
SELECT fruit
FROM fruit
WHERE
(country = "USA" AND shipping = "ups")
OR
fruit NOT IN (
SELECT fruit FROM fruit WHERE country = "USA"
)
GROUP BY fruit
所以,如果我没错,第一个语句将返回apple
。第二个有一个嵌套查询,它将返回apple
和banana
,因为它们都有国家美国,并且语句将找到不在此组中的水果,因此它是berry
。因此,最终它只会返回apple
和berry
如果它回应了我将在我的计算机上尝试的一切。
答案 1 :(得分:0)
尝试这个
SELECT fruit FROM fruit WHERE (shipping = 'ups' AND country = 'USA') OR country NOT IN('USA');
答案 2 :(得分:0)
尝试这个
从水果中选择水果WHERE(运费=' ups' AND country =' USA')或国家/地区! ='美国' GROUP BY水果;
答案 3 :(得分:0)
你可以写这个
SELECT fruit FROM fruit WHERE (shipping = 'ups' AND country = 'USA') OR fruit NOT IN (SELECT fruit FROM fruit WHERE country = 'USA' GROUP BY fruit);
答案 4 :(得分:0)
SELECT DISTINCT f.fruit
FROM TableFruit AS f
LEFT OUTER JOIN TableFruit AS usa ON f.fruit=usa.fruit and usa.country='USA'
WHERE f.shipping = 'ups' AND f.country ='USA'
OR usa.country IS NULL
说明:如果没有满足连接条件的行,OUTER JOIN将为连接表中的所有列返回空值。在这种情况下,它正在寻找与b
和美国的国家/地区具有相同成果的行f
。因此,如果没有国家/地区的行,则国家/地区将为空。