[英]Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous
[英]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_id
或sce.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.