2015-10-19 187 views
-5

我試圖根據使用USER ID作爲4和公司ID爲4來拉3個電話簿。我目前的查詢似乎是拉他們幾次。我怎樣才能得到它只拉一次記錄?MySQL Query LEFT JOIN 5表

SELECT 
    users.ID, 
    ownership_company_managers.*, 
    company_user.*, 
    phonebook_list.*, 
    ownership_phonebook.* 
FROM users LEFT JOIN 
(
    phonebook_list, ownership_phonebook, ownership_company_managers, company_user) ON (
    users.ID=ownership_company_managers.USER_ID 
    AND ownership_company_managers.USER_ID=ownership_company_managers.COMPANY_ID 
    AND ownership_company_managers.COMPANY_ID=company_user.COMPANY_ID 
    AND company_user.USER_ID=company_user.COMPANY_ID 
) 
WHERE users.ID='4' 

enter image description here

enter image description here

enter image description here

enter image description here

​​

+2

爲什麼圖片而不只是文字 – Drew

+0

圖片更容易 –

+0

我想不把一個sqlfiddle放在一起更容易:) – Drew

回答

1
SELECT users.ID, ownership_company_managers.*, company_user.*, 
     phonebook_list.*, ownership_phonebook.* 

FROM users 
LEFT JOIN phonebook_list ON users.ID = ownership_company_managers.USER_ID 
LEFT JOIN ownership_phonebook ON ... , 
LEFT JOIN ... 
LEFT JOIN ... 

WHERE users.ID='4'