所以我目前得到這個查詢:MySQL查詢到從關係表中檢索數據並篩選出重複
SELECT user_expertise.user_id, user_expertise.expertise_id
FROM user_expertise
INNER JOIN user_locations ON user_expertise.user_id = user_locations.user_id
WHERE user_expertise.expertise_id!=$exid AND user_locations.location_id = $_SESSION["user"]["location"]["location_id"]
ORDER BY user_expertise.user_id
$exid
是專業知識的電流id和$_SESSION["user"]["location"]["location_id"]
是從會話檢索到的當前位置標識。爲了這個例子,我們假設$exid = 3981
和$_SESSION["user"]["location"]["location_id"] = 24
。
我想只檢索附加到它們的沒有expertise_id
的$exid (3981)
的用戶。目前的問題是,附有此ID的用戶在他們還附加了另一個用戶時會顯示出來。假設用戶user_id
22
有3981
和6523
。在這種情況下,我不希望他成爲結果的一部分,但他是。起初他沒有被選中,因爲他有附加在他身上的3981
,但隨後他被選中,因爲他也附有6523
。
更新:所以我對用於執行查詢的代碼感到有些生氣,並在新的代碼中重寫了其中的一個,因此我可以傳入完整的查詢字符串。現在我可以嘗試所有的想法而不會感到頭疼。
我對你的總體建議是學習如何在PHP中使用例如mysqli或PDO庫來準備sql語句。你現在看起來像意大利麪條低質量的代碼。 – Jacobian
你的命名很混亂。 '$ exid'(eww)是「Expertise ID」的簡稱,但您將其與名爲'user_id'的字段進行比較。這是什麼?我想我們的代碼命名是第一步,然後使用不同的'JOIN'或者添加'WHERE事件NOT IN(SELECT thing FROM other_thing)'類型的子查詢。 – moopet
對不起moopet,應該是expertise_id。當我複製代碼時發生錯誤 –