简体   繁体   English

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

[英]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_idsce.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_idsce.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.

相关问题 Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous 完整性约束违规:1052 where子句中的列'id'不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous SQLSTATE [23000]:完整性约束违规:1052 where子句中的列'NRP'不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'NRP' in where clause is ambiguous SQLSTATE[23000]:违反完整性约束:1052 列 'created_at' 在 where 子句中不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous 错误代码:1052。where子句中的列“ datetime”不明确 - Error Code: 1052. Column 'datetime' in where clause is ambiguous MySQL错误:冲突:1052 where子句不明确的列'created_at' - Mysql error: violation: 1052 Column 'created_at' in where clause is ambiguous' 错误 1052:where 子句中的列“metric_name”不明确 - Error 1052: Column 'metric_name' in where clause is ambiguous mysql错误:#1052-from子句中的列“ id”不明确 - mysql error: #1052 - Column 'id' in from clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM