我在MySQL數據庫 表下表結構來獲取數據:MYTABLE如何通過在MySQL
--------------------------------
| 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和專案的組合這個
| 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和專案組合不正確地匹配
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值。我的查詢出了什麼問題?我的查詢是否正確? 請幫幫我。
在此先感謝。
問題不清楚!看看你提供的第三張表格,有兩個記錄,3 - 1 - 1這應該如何獨特? – CloudyMarble