所以我有这段代码:
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语句修改为
select gbg.manual_grade,
gbm.title,
u.user_id,
gbl.grade
它从gradebook_log表中提取所有gbl.grade行,即使我只是尝试过滤掉用户名,但仍然有u.user_id的列,但是用户名没有我正在拉动所有这些额外的行正确匹配..知道为什么会这样吗?
答案 0 :(得分: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'
答案 1 :(得分:-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
认为您可能想要将外键添加到用户中(如果有的话,可以使用课程)并且可以尝试一下