SQL - 加入3个实体表和2个关系表

时间:2015-12-12 00:10:38

标签: sql sql-server sql-server-2012 relational-database

我正在尝试为3个实体表和2个关系表编写SQL Join。

实体表是:

  • TJobs
  • TCustomers
  • TMaterials

关系表是:

  • TJobStatuses
  • TJobMaterials

此查询返回正确的结果,但它们重复了4次:

SELECT
 TJS.intJobStatusID
,TJS.strStatus
,TJ.intJobID
,TC.strLastName + ', ' TC.strFirstName AS strCustomerName
,TC.intCustomerID
,TJ.dtmStartDate
,TJM.intMaterialQuantityID
,TM.intMaterialID
,TM.monCost
FROM
 TJobStatuses             AS TJS
,TJobs                    AS TJ
,TCustomers               AS TC
,TMaterials               AS TM
,TJobMaterials            AS TJM
WHERE
    TJS.intJobStatusID    = 3
AND TJ.intCustomerID      = TC.intCustomerID
AND TJ.intCustomerID      = 2
AND TM.intMaterialID      = TJM.intMaterialID
ORDER BY
 TJ.intJobID
,TM.intMaterialID

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

要使用内部联接,您需要选择一个ID以将每个表链接到一系列排序。

public class Report {
  public void unlinkFromStudent() {
    if(this.student != null) {
      this.student.getReports().remove(this);
      this.student = null;
    }
  }
}