[英]ERROR 1052 (23000): Column 'course_id' in where clause is ambiguous
I'm new to MySQL so please tell me if my question is missing information,我是 MySQL 的新手,所以请告诉我我的问题是否缺少信息,
I have a query that works fine:我有一个工作正常的查询:
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';
But when I want to add another column from a different table:但是当我想从不同的表中添加另一列时:
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';
I get this error我收到这个错误
ERROR 1052 (23000): Column 'course_id' in where clause is ambiguous
Anyone know why?有谁知道为什么?
Thanks谢谢
This is because the column course_id
is present in more than two tables.这是因为列
course_id
存在于两个以上的表中。
Write sm.course_id
or sce.course_id
and it will work.写
sm.course_id
或sce.course_id
它将起作用。
You are joining multiple tables, at least two of the tables have the column course_id
.您要加入多个表,其中至少有两个表具有
course_id
列。 In your statement and course_id = 'MyCourse'
you are not specifying which table has course_id.在您的语句
and course_id = 'MyCourse'
您没有指定哪个表具有 course_id。
student_courseenrollment
and one of your other tables both have a column called course_id
. student_courseenrollment
和您的其他表之一都有一个名为course_id
的列。 Use a table alias, eg au.course_id
.使用表别名,例如
au.course_id
。
you need to use the alias of the table with the column of course id in it您需要将表的别名与其中的课程 id 列一起使用
like sce.course_id
像
sce.course_id
as stated in the comment below this might change your result so use the table name of the table of that is used in the where clause or the alias of that table如下面的评论中所述,这可能会改变您的结果,因此请使用 where 子句中使用的表的表名或该表的别名
You are using two different tables with same column name.您正在使用两个具有相同列名的不同表。 If you want to use that column name in any query, you should use with table name Eg:
如果您想在任何查询中使用该列名,您应该使用表名,例如:
select * from table1,table2 where table1.userId='any id';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.