为什么我不能在此查询中进行任何类型的连接?

Why I can't make any kind of join in this query?

当我尝试进行任何类型的连接时,它给我错误

我该如何解决这个问题

这是我的代码,因为这个问题对我来说很麻烦

这让我很烦,因为我不知道我错了什么

那么,请您指导我如何在此查询中进行任何类型的联接?

Create Table Department (
dept_name varchar(100) Primary Key,
building varchar(100),
budget INT,
);


Create Table student (
ID INT Primary Key,
name varchar(100),
dept_name varchar(100),
tot_cred INT,

Foreign Key (dept_name) references Department
);

Create Table course (
course_id varchar(100) Primary Key,
title varchar(100),
dept_name varchar(100),
credits INT,
Foreign Key (dept_name) references Department
);

Create Table takes (
ID INT,
course_id varchar(100),
sec_id INT,
semester varchar(100),
year INT,
grade varchar(100),

Primary Key (ID, course_id, sec_id, semester, year)

);
 --inserting student values
Insert Into student (ID, name, dept_name, tot_cred) values (00128, 'Zhang', 'Comp.Sci', 102);
Insert Into student (ID, name, dept_name, tot_cred) values (12345, 'Shankar', 'Comp.Sci', 32);
Insert Into student (ID, name, dept_name, tot_cred) values (19991, 'Brandt', 'History', 80);
Insert Into student (ID, name, dept_name, tot_cred) values (23121, 'Chavez', 'Finance', 110);
Insert Into student (ID, name, dept_name, tot_cred) values (44553, 'Peltier', 'Physics', 56);
Insert Into student (ID, name, dept_name, tot_cred) values (45678, 'Levy', 'Physics', 46);
Insert Into student (ID, name, dept_name, tot_cred) values (54321, 'Williams', 'Comp.Sci', 54);
Insert Into student (ID, name, dept_name, tot_cred) values (55739, 'Sanchez', 'Music', 38);
Insert Into student (ID, name, dept_name, tot_cred) values (70557, 'Snow', 'Physics', 0);
Insert Into student (ID, name, dept_name, tot_cred) values (76543, 'Brown', 'Comp.Sci', 58);
Insert Into student (ID, name, dept_name, tot_cred) values (76653, 'Aoi', 'Elec.Eng', 60);
Insert Into student (ID, name, dept_name, tot_cred) values (98765, 'Bourikas', 'Elec.Eng', 98);
Insert Into student (ID, name, dept_name, tot_cred) values (98988, 'Tanaka', 'Biology', 120);

--inserting takes values
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (00128, 'CS-101', 1, 'Fall', 2009, 'A');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (00128, 'CS-347', 1, 'Fall', 2009, 'A-');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (12345, 'CS-101', 1, 'Fall', 2009, 'C');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (12345, 'CS-190', 2, 'Spring',2009, 'A');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (12345, 'CS-315', 1, 'Spring', 2010, 'A');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (12345, 'CS-347', 1, 'Fall', 2009, 'A');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (19991, 'HIS-351', 1, 'Spring', 2010, 'B');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (23121, 'FIN-201', 1, 'Spring', 2010, 'C+');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (44553, 'PHY-101', 1, 'Fall', 2009, 'B-');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (45678, 'CS-101', 1, 'Fall', 2009, 'F');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (45678, 'CS-101', 1, 'Spring', 2010, 'B+');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (45678, 'CS-319', 1, 'Spring', 2010, 'B');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (54321, 'CS-101', 1, 'Fall', 2009, 'A-');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (54321, 'CS-190', 2, 'Spring', 2009, 'B+');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (55739, 'MU-199', 1, 'Spring', 2010, 'A-');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (76543, 'CS-101', 1, 'Fall', 2009, 'A');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (76543, 'CS-319', 2, 'Spring', 2010, 'A');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (98765, 'CS-101', 1, 'Fall', 2009, 'C-');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (98765, 'CS-315', 1, 'Spring', 2010, 'B');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (98988, 'BIO-101', 1, 'Summer', 2009, 'A');
Insert Into takes (ID, course_id, sec_id, semester, year, grade) values (98988, 'BIO-101', 1, 'Summer', 2010, 'null');

--inserting course values
Insert Into Course(course_id, title, dept_name, credits) values ('BIO-101', 'Intro to Biology', 'Biology', 4);
Insert Into Course(course_id, title, dept_name, credits) values ('BIO-301', 'Genetics', 'Biology', 4);
Insert Into Course(course_id, title, dept_name, credits) values ('BIO-399', 'Computational Biology', 'Biology', 3);
Insert Into Course(course_id, title, dept_name, credits) values ('CS-101', 'Intro to Computer Science', 'Comp.Sci', 4);
Insert Into Course(course_id, title, dept_name, credits) values ('CS-190', 'Game Design', 'Comp.Sci', 4);
Insert Into Course(course_id, title, dept_name, credits) values ('CS-315', 'Robotics', 'Comp.Sci', 3);
Insert Into Course(course_id, title, dept_name, credits) values ('CS-319', 'Image Processing', 'Comp.Sci', 3);
Insert Into Course(course_id, title, dept_name, credits) values ('CS-347', 'Database System Concepts', 'Comp.Sci', 3);
Insert Into Course(course_id, title, dept_name, credits) values ('EE-181', 'Intro to Digital System', 'Elec.Eng', 3);
Insert Into Course(course_id, title, dept_name, credits) values ('FIN-201', 'Investment Banking', 'Finance', 3);
Insert Into Course(course_id, title, dept_name, credits) values ('HIS-351', 'World History', 'History', 4);
Insert Into Course(course_id, title, dept_name, credits) values ('MU-199', 'Music Video Production', 'Music', 3);
Insert Into Course(course_id, title, dept_name, credits) values ('PHY-101', 'Physics Principles', 'Physics', 4);

--inserting department values

Insert Into Department(dept_name, building, budget) values ('Biology', 'Watson', 90000);
Insert Into Department(dept_name, building, budget) values ('Comp.Sci', 'Taylor', 100000);
Insert Into Department(dept_name, building, budget) values ('Elec.Eng', 'Taylor', 85000);
Insert Into Department(dept_name, building, budget) values ('Finance', 'Painter', 120000);
Insert Into Department(dept_name, building, budget) values ('History', 'Painter', 50000);
Insert Into Department(dept_name, building, budget) values ('Music', 'Packard', 80000);
Insert Into Department(dept_name, building, budget) values ('Physics', 'Watson', 70000);


Select name, dept_name

From student join Department

on student.dept_name=Department.dept_name

看看 dept_name,你有 dept_name 部门和学生

所以,请尝试更改您的查询,指定您想要的dept_name:

Select a.name, b.dept_name

From student a join Department b 

on student.dept_name=Department.dept_name

顺便说一下,我建议您在此处 post 您的错误消息,以便我们可以更深入地查看错误。

当我运行你的查询时我收到了这个错误ERROR: column reference "dept_name" is ambiguous

我不确定这是你的问题还是拼写错误,但要修复它,你只需指定要从中检索 dept_name 的 table,如下所示:

select name, Department.dept_name                                                                                                                                                                          
from student join Department                                                                                                                                                                                    
on student.dept_name=Department.dept_name;

哪个returns:

   name   | dept_name 
----------+-----------
 Zhang    | Comp.Sci
 Shankar  | Comp.Sci
 Brandt   | History
 Chavez   | Finance
 Peltier  | Physics
 Levy     | Physics
 Williams | Comp.Sci
 Sanchez  | Music
 Snow     | Physics
 Brown    | Comp.Sci
 Aoi      | Elec.Eng
 Bourikas | Elec.Eng
 Tanaka   | Biology