简体   繁体   中英

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,

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.

Write sm.course_id or sce.course_id and it will work.

You are joining multiple tables, at least two of the tables have the column course_id . In your statement and course_id = 'MyCourse' you are not specifying which table has course_id.

student_courseenrollment and one of your other tables both have a column called course_id . Use a table alias, eg au.course_id .

you need to use the alias of the table with the column of course id in it

like 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

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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