sql语句无法获取错误

时间:2018-05-23 15:40:23

标签: mysql oracle

我在发表声明时遇到一些麻烦,我正在寻找每个学生的总学分和学生证。

我写道:

select student.ID,sum (course.credits)
from (student natural join takes),course 
where takes.course_id=course.course_id
group by student.ID

当我运行此操作时,我收到以下错误: NATURAL join中使用的列不能有限定符

我的桌子是

create table student
(ID             varchar(5),
 name           varchar(20) not null,
 dept_name      varchar(20), 
 tot_cred       numeric(3,0) check (tot_cred >= 0),
 primary key (ID),
 foreign key (dept_name) references department
    on delete set null

create table takes
(ID              varchar(5), 
 course_id           varchar(8),
 sec_id              varchar(8), 
 semester            varchar(6),
 year                numeric(4,0),
 grade                   varchar(2),
 primary key (ID, course_id, sec_id, semester, year),
 foreign key (course_id,sec_id, semester, year) references section
    on delete cascade,
 foreign key (ID) references student
    on delete cascade

我不确定问题是什么。当我运行此语句时,它可以正常工作

select name, sum(course.credits)
from (student natural join takes),course 
where ID=1000 and takes.course_id=course.course_id
group by name

如果你能指出我正确的方向我会很感激..谢谢你

2 个答案:

答案 0 :(得分:1)

建议不要使用

NATURAL JOIN,这可能会导致细微的意外错误,尤其是在以后修改表格时。请改用INNER JOIN

select name, sum(c.credits)
  from student s
  inner join takes t
    ON t.ID = s.ID
  inner join course c
    ON c.COURSE_ID = t.COURSE_ID
  where s.ID = 1000
  group by name

祝你好运。

答案 1 :(得分:0)

关于如何使用括号,from子句是错误的。只需对所有联接使用明确的join子句,您应该没问题:

SELECT       student.ID, SUM(course.credits)
FROM         student
NATURAL JOIN takes
NATURAL JOIN course 
GROUP BY     student.ID