2016-05-10 118 views
-1

檢索重複的記錄我有四個表:SQL查詢,以便從四個表

  • 部(DepartmentID的)
  • 場(CourseId,DepartmentID的)
  • CourseInstructor(CourseId,InstructorId)
  • 導師( InstructorId,名字,姓氏,出租日期)

我正在研究asp.net mvc。教師模型是 -

public class Instructor 
{ 
    public int InstructorId {get; set;} 
    public string FristName {get; set;} 
    public string LastName {get; set;} 
    public DateTime HireDate {get; set;} 
    public string FullName 
    { 
    get 
    { 
     return FirstName + " " + LastName; 
    } 
    } 

現在我需要根據給定的DepartmentId找到Department的所有不同教師記錄。

我寫了下面的SQL查詢,但它說 -

不明確的列名 'InstructorId'

string query = "select InstructorId, MAX(FirstName), MAX(LastName) from Instructor " 
      + "left join CourseInstructor on CourseInstructor.InstructorId = Instructor.InstructorId " 
      + "left join Course on Course.CourseId = CourseInstructor.CourseId " 
      + "left join Department on Department.DepartmentId=Course.DepartmentId " 
      + "where [email protected] " 
      + "group by Instructor.InstructorId"; 
     IEnumarable<Instructor> Instructors = db.Database.SqlQuery<Instructor>(query, id); 

這裏id是給出DepartmentID的。我該如何解決這個問題。

+1

使用合格的列名(即總是帶有一個表別名),你將永遠不會*有這個prbolem。 –

+1

最好問一個新問題,而不是編輯舊問題。爲原始問題提供的答案不再有意義。這會混淆跟隨你的人。 –

回答

1

使用聚集的嫌疑。下面是一個使用exists一種替代方案:

select i.* 
from Instructor i 
where exists (select 1 
       from CourseInstructor ci join 
        Course c 
        on c.CourseId = ci.CourseId 
       where ci.InstructorId = i.InstructorId and 
        c.DepartmentId = @p0 
      ); 

注:

  • 這個版本並不需要聚集,所以它應該會更快。
  • joinDepartment是不必要的,因爲id在Course表中。
  • left join s是不必要的,因爲您的查詢需要在DepartmentId上匹配。
  • 表別名使查詢更易於編寫和閱讀。
  • 完全限定的列名可以防止您遇到的問題。
+0

謝謝。它工作正常。 –

0

由於InstructorId出現在多個表中,SQL Server不知道要返回哪個表。有兩種方法可以解決這個問題。

您可以在列名前添加表名。所以SELECT InstructorId...變成SELECT Instructor.InstructorId...

或者您可以減少鍵入數量aliasing您的表格,並在列名稱前附加別名。

select 
    i.InstructorId, 
    MAX(i.FirstName), 
    MAX(i.LastName) 
from 
    Instructor AS i 
     left outer join CourseInstructor AS ci   on ci.InstructorId = i.InstructorId 
     left outer join Course on AS c     on c.CourseId  = ci.CourseId 
     left outer join Department AS d    on d.DepartmentId = c.DepartmentId 
where 
    [email protected] 
group by 
    i.InstructorId 
; 
0

需要判定是存在於兩個或多個選擇表中的所有列名:

string query = "select Instructor.InstructorId, MAX(Instructor.FirstName), MAX(Instructor.LastName) from Instructor " 
       + "left join CourseInstructor on CourseInstructor.InstructorId = Instructor.InstructorId " 
       + "left join Course on Course.CourseId = CourseInstructor.CourseId " 
       + "left join Department on Department.DepartmentId=Course.DepartmentId " 
       + "where [email protected] " 
       + "group by Instructor.InstructorId"; 
      IEnumarable<Instructor> Instructors = db.Database.SqlQuery<Instructor>(query, id); 
0

下面查詢使用

string query = "select Instructor.InstructorId, MAX(FirstName), MAX(LastName) from Instructor " 
     + "left join CourseInstructor on CourseInstructor.InstructorId = Instructor.InstructorId " 
     + "left join Course on Course.CourseId = CourseInstructor.CourseId " 
     + "left join Department on Department.DepartmentId=Course.DepartmentId " 
     + "where [email protected] " 
     + "group by Instructor.InstructorId";