繁体   English   中英

错误 1052 (23000):where 子句中的列“course_id”不明确

[英]ERROR 1052 (23000): Column 'course_id' in where clause is ambiguous

我是 MySQL 的新手,所以请告诉我我的问题是否缺少信息,

我有一个工作正常的查询:

select au.email, sm.created, sm.grade, sm.max_grade
from auth_user au, courseware_studentmodule sm
where sm.student_id = au.id
and course_id = 'MyCourse'
and sm.module_type = 'problem';

但是当我想从不同的表中添加另一列时:

select au.email, sm.created, sce.created , sm.grade, sm.max_grade
from auth_user au, courseware_studentmodule sm,  student_courseenrollment sce
where sm.student_id = au.id and sm.student_id = sce.id
and course_id = 'MyCourse'
and sm.module_type = 'problem';

我收到这个错误

ERROR 1052 (23000): Column 'course_id' in where clause is ambiguous

有谁知道为什么?

谢谢

这是因为列course_id存在于两个以上的表中。

sm.course_idsce.course_id它将起作用。

您要加入多个表,其中至少有两个表具有course_id列。 在您的语句and course_id = 'MyCourse'您没有指定哪个表具有 course_id。

student_courseenrollment和您的其他表之一都有一个名为course_id的列。 使用表别名,例如au.course_id

您需要将表的别名与其中的课程 id 列一起使用

sce.course_id

如下面的评论中所述,这可能会改变您的结果,因此请使用 where 子句中使用的表的表名或该表的别名

您正在使用两个具有相同列名的不同表。 如果您想在任何查询中使用该列名,您应该使用表名,例如:

select * from table1,table2 where table1.userId='any id';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM