MySQL Query LEFT JOIN 5表

时间:2015-10-19 05:01:45

标签: php mysql

我试图基于使用USER ID为4并且公司ID为4来提取3个电话簿。我当前的查询似乎是多次拉动它们。我怎么能让它只拉一次3条记录?

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

enter image description here

1 个答案:

答案 0 :(得分: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'