我正在尝试掌握窍门,NOT EXISTS
但遇到了一些麻烦。假设我有 2 张桌子。
Employees:
+------+------+
| eid | name |
+------+------+
| 1 | Bob |
| 2 | Alice|
| 3 | Jill |
+------+------+
Transactions:
+----------+----------+----------+-----------+
| tid | eid | type | amount |
+----------+----------+----------+-----------+
| 1 | 1 | Deposit | 50 |
| 2 | 1 | Open | 500 |
| 3 | 3 | Open | 200 |
| 4 | 2 | Withdraw | 25 |
| 5 | 2 | Open | 100 |
+----------+----------+----------+-----------+
假设我要查找所有未开立账户且金额为 250 美元或更高的员工的姓名。这意味着我只想要员工开设金额 < 250 美元的账户的行。现在我有这样的东西......
SELECT name FROM Employees e
WHERE NOT EXISTS (
SELECT * FROM Transactions t
WHERE t.type <> 'Open' AND t.amount >= 250 AND t.eid = e.eid);
这显然是错误的,我真的不明白为什么。