例如,有这样的表“ Eaten_food”:
Name Food
--------------------
Lisa Grapes
Victoria Broccoli
Oliver Carrot
Lisa Apple
Kayla Pineapple
Oliver Banana
Victoria Kiwi
Kayla Grapes
我想做一个查询,以仅显示不吃葡萄的名字。
例如,Kayla和Lisa吃了葡萄,因此它们不应出现在结果列表中。
SELECT Name FROM Eaten_food WHERE NOT Food = "Grapes" GROUP BY Name
此查询无效。在这种情况下,什么查询应该是正确的?
答案 0 :(得分:0)
ID | SN | Admdate | Disdate |
+------+-------+-------------+---------------+
|**212 | 1 | 2014-01-01 | 2014-01-12** |
|201 | 2 | 2014-01-01 | 2014-01-10 |
|**212 | 598 | 2014-01-28 | 2014-02-12** |
|**212 | 10000 | 2016-12-23 | 2016-12-29 ** |
| **212| 15112 | 2017-06-10 | 2017-06-21 ** |
|*401 | 20101 | 2018-01-01 | 2018-01-11 |*
|*401 | 21101 | 2018-02-01 | 2018-02-13 |*
|401 | 22101 | 2018-10-01 | 2018-10-11 |
还有许多其他方式可以编写HAVING子句。其中一些是:
SELECT Name
FROM Eaten_food
GROUP BY Name
HAVING BIT_OR(Food = "Grapes") = 0
他们都检查给定组(HAVING BIT_AND(Food <> "Grapes")
HAVING SUM(Food = "Grapes") = 0
HAVING MAX(Food = "Grapes") = 0
)的Food
列中的值是否永远都不是“葡萄”。
上述条件适用于MySQL,因为布尔条件返回Name
或0
。一个标准的SQL条件可能是:
1
,并且适用于任何主要的RDBMS。
如果您的表(例如HAVING MAX(CASE Food WHEN 'Grapes' THEN 1 ELSE 0 END) = 0
)具有不同的名称,则可以使用NOT EXISTS子查询条件:
people
取决于数据,这可能会更快,因为SELECT p.Name
FROM people p
WHERE NOT EXISTS (
SELECT *
FROM Eaten_food f
WHERE f.Name = p.Name
AND f.Food = 'Grapes'
)
不需要完全读取。
等效查询是:
Eaten_food
所有查询都将受益于SELECT p.Name
FROM people p
LEFT JOIN Eaten_food f
ON f.Name = p.Name
AND f.Food = 'Grapes'
WHERE f.Name is NULL
上的综合索引。
答案 1 :(得分:0)
您可以尝试一下,
SELECT Name FROM Eaten_food WHERE NOT Food = "Grapes" and Name not in (select Name from Eaten_food where Food="Grapes") GROUP BY Name;
答案 2 :(得分:0)
directory = os.listdir(self.hook.full_path)
或类似的东西