以下代码将用于从两个表中选择数据:
SELECT t1.foo, t2.bar FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON t1.foo=t2.foo
Run Code Online (Sandbox Code Playgroud)
我可以这么容易写
SELECT t2.foo, t2.bar FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON t1.foo=t2.foo
Run Code Online (Sandbox Code Playgroud)
t1.foo或者t2.foo:一个或六个中的六个.为什么不foo呢?
我一直在想,为什么SQL服务器只是在没有指定一个表或另一个表的情况下自动返回数据,因为选择完全是任意的(据我所知).
我可以编写一个你需要指定表格的场景,例如
SELECT t1.foo, t2.bar FROM TABLE1 t1 INNER JOIN TABLE t2 ON t1.foo+=t2.foo
Run Code Online (Sandbox Code Playgroud)
但是,根据我的经验,这种情况远非常态.
任何人都可以告诉我为什么语言的设计,以便我必须在我的代码中做出这个看似随意的决定?
我是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';
Run Code Online (Sandbox Code Playgroud)
但是当我想从另一个表中添加另一个列时:
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';
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
ERROR 1052 (23000): Column 'course_id' in where clause is ambiguous
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?
谢谢