2013-07-05 79 views
2

所以我有這樣的代碼:表連接的每一行返回從整個表

select gbg.manual_grade, 
       gbm.title, 
       u.user_id 
from gradebook_grade as gbg 

join course_users as cu on gbg.course_users_pk1 = cu.pk1 
join users as u on cu.users_pk1 = u.pk1 
join gradebook_main as gbm on gbg.gradebook_main_pk1 = gbm.pk1 
join course_main as cm on gbm.crsmain_pk1 = cm.pk1 

where (cm.course_id) = 'HUMA-1301-057IN-S12013' and (u.user_id) = 'bpugh' 

而且,作爲代碼現在,結果是完美的我想要什麼。這些記錄是唯一的,它們與正確的用戶標識和分配對齊。但是還有一個我需要獲得的專欄。

每當我加入這一行:

join gradebook_log as gbl on gbm.pk1 = gbl.gradebook_main_pk1 

和修改我的select語句是

select gbg.manual_grade, 
       gbm.title, 
       u.user_id, 
       gbl.grade 

它拉所有gbl.grade行從gradebook_log表,即使我「米試圖只過濾用戶名,但仍然有u.user_id列,但用戶名不能正確匹配所有這些額外的行,我現在拉..任何想法,爲什麼這是?

+0

@Scotch'join' ='內部連接'在SQL Server中。功能是一樣的,它只是不同的語法。 –

+1

你能否提供一些樣本數據/表格結構?它拉動所有符合條件的行,或許在gradebook_log連接中還有另一個字段。 –

+0

我已經多次檢查條件和密鑰。我幾乎積極的加入條件是正確的。 gradebook_log表依賴於gradebook_main_k1和users_pk1的主鍵進行連接。 – user1275954

回答

1

缺少連接標準的新表,看起來應該是:

select gbg.manual_grade, 
       gbm.title, 
       u.user_id, 
       gbl.grade 
from gradebook_grade as gbg  
join course_users as cu on gbg.course_users_pk1 = cu.pk1 
join users as u on cu.users_pk1 = u.pk1 
join gradebook_main as gbm on gbg.gradebook_main_pk1 = gbm.pk1 
join course_main as cm on gbm.crsmain_pk1 = cm.pk1 
join gradebook_log as gbl 
     on gbm.pk1 = gbl.gradebook_main_pk1 
     AND u.pk1 = gbl.users_pk1 
where (cm.course_id) = 'HUMA-1301-057IN-S12013' and (u.user_id) = 'bpugh' 
+0

這很好,謝謝。 – user1275954

-1
join gradebook_log as gbl 
     on gbm.pk1 = gbl.gradebook_main_pk1 
and gbl.User_ForeignKey1 = u1.pkl 
and gbl.CourseMain_ForeignKey2 = cm.pk1 

覺得你可能要添加的外鍵的用戶和(coursemain如果有的話),也許給它一個shot

相關問題