简体   繁体   English

INNER JOIN 4桌

[英]INNER JOIN with 4 tables

I'm having a problem inner joining multiple tables. 我在内部连接多个表时遇到问题。 When I join any of the three tables it returns the results correctly. 当我加入三个表中的任何一个时,它都会正确返回结果。 For instance: 例如:

SELECT jobLink.*
     , trucks.*
     , employees.* 
  FROM jobLink 
  JOIN schedule 
    ON jobLink.jobID = schedule.id 
  JOIN employees 
    ON jobLink.empID = employees.id 
 WHERE jobLink.thedate = '2014-18-07' 
   AND schedule.id =  439

Will return the jobname and all the employees on the job under it. 将返回工作名称及其下的所有员工。

As soon as I join the last join I get an empty result leading me to believe that my order is off or something. 加入最后一个加入后,我得到的结果是空的,使我相信我的订单已结清。 I'm open to other suggestions as there is possibly a better or more efficient way to accomplish this so I'm listing my tables below. 我愿意接受其他建议,因为可能有更好或更有效的方法来完成此操作,因此在下面列出了我的表格。 Any help is appreciated. 任何帮助表示赞赏。

jobLink table: jobLink表:

id     |    jobID    |   empID   |   copy   |   truck   |    thedate
_____________________________________________________________________

employees table: 员工表:

id     |    name    |   abb   |   pos   |   current   
_____________________________________________________________________

trucks table: 卡车表:

id     |    num    |   sort  
_____________________________________________________________________ 

schedule table: 时间表表:

id     |    name    |   address   |   city   |   phone   |    email
_____________________________________________________________________

My query is currently: 我的查询当前是:

 SELECT jobLink.*
      , trucks.*
      , employees.* 
   FROM  jobLink   
   JOIN  schedule 
     ON jobLink.jobID = schedule.id 
   JOIN employees 
     ON jobLink.empID = employees.id 
   JOIN trucks 
     ON jobLink.truck = trucks.id 
  WHERE jobLink.thedate = '2014-18-07' 
    AND schedule.id = 439

Each table has a different quantity of results. 每个表都有不同数量的结果。 Schedule has 1 result but the rest will vary depending on how many employees and trucks are on the job (from the schedule). Schedule有1个结果,但其余结果将根据工作中有多少名员工和卡车而有所不同(来自Schedule)。 Im trying to produce something like this: 我试图产生这样的东西:

在此处输入图片说明

the employees wont end up under the trucks because i dont have that set up in the db tables but can be listed before or after the employee list. 员工最终不会在卡车下面,因为我没有在db表中进行设置,但可以在员工列表之前或之后列出。 Thanks in advance! 提前致谢!

yes, the output is php sorry for not being more specific... @Strawberry after speaking to a colleague of mine, his suggestion is to change the structure. 是的,输出的结果是php抱歉,没有具体说明... @Strawberry与我的一位同事交谈后,他的建议是更改结构。 By changing the structure and adding a job_truck table and a job_employees table I am able to accomplish what I need by using inner joins. 通过更改结构并添加一个job_truck表和一个job_employees表,我可以通过使用内部联接来完成所需的工作。 @RubahMalam yes i did try left joins but i am unable to accomplish it in one query. @RubahMalam是的,我确实尝试过左联接,但是我无法在一个查询中完成它。 Thank you for the suggestions. 感谢您的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM