0
基本上我有4個表和當前組的組ID。 我將列出的重要領域和信息:困惑複雜加入在MySQL請求
Users:
ID,
FirstName,
LastName
Groups:
ID,
participantlist_id
participantlist:
list_id
participantlist_links:
list_id,
participant_id
我已經想出如何證明是在樹的一個小功能,最高點participantlist.list_id所有用戶:
SELECT Users.ID,Users.FirstName,Users.LastName FROM Users
JOIN participantlist_links ON Users.ID = participantlist_links.participant_id
JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
JOIN Groups ON participantlist.list_id = Groups.participantlist_id AND
Groups.ID = '".workspaces_tell_me_brainstorm_group_id_from_group_id($group_id)."'
現在我想排除所有鏈接到當前組(GROUP_ID $)的participantlist.list_id的用戶,但我不能想出一個好辦法做到這一切在一個單一的查詢。我認爲解決的辦法將是某種形式的左連接/右或左/右外連接比較。
我的臨時工作,但非常昂貴的時間解決方案是這樣的:
SELECT table2.id as ID, table2.firstname as FirstName, table2.lastname as LastName
FROM
(SELECT table1.ID,table1.FirstName,table1.LastName FROM Users as table1
JOIN participantlist_links ON table1.ID = participantlist_links.participant_id
JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
JOIN Groups ON participantlist.list_id = Groups.participantlist_id
AND Groups.ID = '".workspaces_tell_me_brainstorm_group_id_from_group_id($group_id)."')
as table2
LEFT JOIN
(SELECT table4.ID,table4.FirstName,table4.LastName FROM Users as table4
JOIN participantlist_links ON table4.ID = participantlist_links.participant_id
JOIN participantlist ON participantlist_links.list_id = participantlist.list_id
JOIN Groups ON participantlist.list_id = Groups.participantlist_id
AND Groups.ID = '".$group_id."')
as table3
ON table2.ID = table3.ID WHERE table3.id is null
有什麼建議?