我需要返回在该组中Items.value完全不等于ZZZ的组。我试着加入item.value <> ='ZZZ'的组中的Items,但它仍返回带有ZZZ的项目的组。我还尝试在选择项目的地方添加一个子选择。value ='ZZZ'不存在,但这也不起作用。
Groups
+----+------+--------------+
| id | name | TAG |
+----+------+--------------+
| 1 | AAA | 1 |
+----+------+--------------+
| 2 | BBB | 1 |
+----+------+--------------+
| 3 | CCC | 1 |
+----+------+--------------+
Items
+----+------+--------------+
| id | Value| Group_id |
+----+------+--------------+
| 1 | XXX | 1 |
+----+------+--------------+
| 2 | YYY | 1 |
+----+------+--------------+
| 3 | ZZZ | 1 |
+----+------+--------------+
| 4 | XXX | 2 |
+----+------+--------------+
| 5 | YYY | 2 |
+----+------+--------------+
| 6 | ZZZ | 2 |
+----+------+--------------+
| 7 | XXX | 3 |
+----+------+--------------+
| 8 | YYY | 3 |
+----+------+--------------+
| 9 | OOO | 3 |
+----+------+--------------+
上面的查询应返回
Group
+----+------+--------------+
| 3 | CCC | 1 |
+----+------+--------------+
这不起作用。它返回组ID 1、2和3。
SELECT * FROM Groups g
WHERE NOT EXISTS (
SELECT 1 FROM Items as i
WHERE i.group_id = g.id
AND i.value = 'ZZZ'
);
如何选择上方所有在项目中具有“ ZZZ”的组,然后确定哪些group_id被排除在外? -> 3