doufangpian5545
2015-10-19 05:01
浏览 75
已采纳

MySQL Query LEFT JOIN 5表

I am trying to pull 3 phonebooks based on using the USER ID as 4 and the COMPANY ID being 4. My current query seems to be pulling them several times. How can I get it to pull just the 3 records once?

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

图片转代码服务由CSDN问答提供 功能建议

我试图根据使用用户ID为4并且公司ID为4来提取3个电话簿。我当前 查询似乎是多次拉他们。 我怎样才能让它只拉出3条记录?

  SELECT 
 users.ID,
 ownership_company_managers。*,
 company_user。*,
 phonebook_list。  *,
 ownership_phonebook。* 
FROM用户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'
   
 
 

< a href =“https://i.stack.imgur.com/zjV1R.png”rel =“nofollow noreferrer”>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpl22899 2015-10-19 06:21
    已采纳
    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'
    
    点赞 评论

相关推荐 更多相似问题