我在 Mysql DB 表中有以下表结构:Mytable
--------------------------------
| uid | locationid | projectid |
--------------------------------
| 2 | 4 | 2 |
| 2 | 4 | 2 |
| 2 | 3 | 5 |
| 2 | 3 | 5 |
| 2 | 1 | 2 |
| 2 | 1 | 2 |
| 2 | 1 | 2 |
| 2 | 1 | 1 |
| 3 | 1 | 5 |
| 3 | 1 | 2 |
| 3 | 1 | 1 |
| 3 | 1 | 1 |
| 7 | 3 | 1 |
| 5 | 1 | 5 |
| 6 | 4 | 2 |
| 5 | 1 | 5 |
| 3 | 4 | 1 |
| 3 | 3 | 3 |
| 7 | 4 | 2 |
| 7 | 4 | 3 |
假设我通过了 uid=2 那么对于 uid=2 locationid 和 projectid 的唯一组合是这样的
| 2 | 4 | 2 |
| 2 | 3 | 5 |
| 2 | 1 | 2 |
| 2 | 1 | 1 |
现在我想要所有具有上述 locationid 和 projectid 组合的 uid 匹配。意味着结果应该是
| 3 | 1 | 2 |
| 3 | 1 | 1 |
| 3 | 1 | 1 |
| 6 | 4 | 2 |
| 7 | 4 | 2 |
如果我通过 uid=3 那么结果应该是
| 2 | 1 | 2 |
| 2 | 1 | 2 |
| 2 | 1 | 2 |
| 2 | 1 | 1 |
| 5 | 1 | 5 |
| 5 | 1 | 5 |
为此,我使用了以下查询,但它给出了错误的结果,因为 locationid 和 projectid 组合不正确匹配
SELECT a.*
FROM Mytable a, Mytable b
WHERE a.locationid = b.locationid
AND a.projectid = b.projectid
AND a.locationid IN (SELECT DISTINCT locationid FROM Mytable WHERE uid=$pmid)
AND a.projectid IN(SELECT DISTINCT projectid FROM Mytable WHERE uid=$pmid)
AND a.uid !=$pmid
这里 $pmid 是我传递的 uid 值。我的查询有什么问题?我的查询是否正确?请帮我。
提前致谢。