假设我有一桌人...
person:
-----------
id | person
---+-------
1 | Jim
2 | Bob
3 | Frank
...我有一张物品表...
item:
----------------
id | item | type
---+------+-----
1 | 21 | 2
2 | 10 | 5
3 | 11 | 1
4 | 9 | 1
...而且我还有一张表格,描述了谁拥有什么...
person_item:
-------------
item | person
-----+-------
1 | 2
2 | 1
3 | 1
如何创建一个查询来告诉我个人何时拥有多个特定类型的项目?我只希望查询与 (1, 2, 3) 类型的项目相关。
查询的结果应采用以下格式:
---------------
person | item
| item
--------+------
person | item
| item
| item
--------+------
... etc.
这是我尝试过的......但它会产生垃圾......
SELECT person.id, item.id FROM person_item AS pi
JOIN item AS i ON i.id = pi.item
JOIN person AS p ON p.id = pi.item
WHERE item.type IN (1,2,3)
HAVING COUNT(pi.person) > 1;