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'

而且,就像现在的代码一样,结果完全是我想要的。这些记录是唯一的,它们与正确的用户 ID 和分配对齐。但我还需要再写一篇专栏。

每当我添加这一行时:

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

并将我的选择语句修改为

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

它从 Gradebook_log 表中提取所有 gbl.grade 行,即使我试图仅按用户名过滤,但仍然有 u.user_id 的列,但是用户名不匹配正确我现在正在拉所有这些额外的行。知道为什么吗?

4

2 回答 2

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'
于 2013-07-05T18:59:17.640 回答
-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

认为您可能想将外键添加到用户和(如果有的话),也许可以试一试

于 2013-07-05T19:12:36.107 回答