我需要一些帮助来解决我的项目的查询。我正在处理的查询需要4个表格。
这些是表格:
Create Table course (code char(11) Primary Key,
name varchar(30),
points int,
check (points >= 1 and points <= 12));
Create Table student (id char(7) Primary Key,
first_name varchar(12),
surname varchar(30),
bsn char(11),
start_date date
);
Create Table aregiven (studentid char(7) References student(id),
coursecode char(11),
assignmentcode char(13),
Primary Key (studentid, coursecode, assignmentcode),
Foreign Key (coursecode, assignmentcode) References
assignment(coursecode, assignmentcode));
Create Table has_completed (studentid char(7) References student(id),
studyprogram_level varchar(10),
studyprogram_name varchar(15),
Primary Key (studentid, studyprogram_name, studyprogram_level)
);
我需要找到已完成学业的学生(first_name&amp;姓氏),并总结学生所遵循的课程的所有要点。 我认为最好的方法是使用子查询来过滤所有完成学习计划的学生,并将每个学生的所有积分相加。
这就是我的想法:
Select s.first_name, s.surname, SUM(c.points)
from course c, aregiven a, (Select * from student, has_completed
where student.id = has_completed.studentid) as s
where c.code = a.coursecode and a.studentid = s.id
group by s.first_name, s.surname;
此查询的唯一问题是并非所有学生都获得了作业。所以这意味着一些完成学业计划的学生不在列表中。
我有点被困,所以我真的很感激一些帮助。
哦,是的,这是我正在使用的数据:
Insert Into student Values
('0817347', 'Adelaide', 'Kemp', '55540-32016', '2012-02-29'),
('0889300', 'Addison', 'Lewis', '62874-60266', '2012-11-14'),
('0838756', 'Chandler', 'Mcgowan', '31573-29574', '2011-10-07'),
('0832660', 'Averi', 'Horn', '57137-29593', '2013-01-28'),
('0820356', 'Paola', 'Montoya', '92971-17968', '2010-11-29'),
('0874162', 'Joslyn', 'Mann', '28097-88031', '2011-10-17'),
('0855759', 'Mckenna', 'Hodge', '10062-01495', '2011-06-03'),
('0861973', 'Ayanna', 'Everett', '72133-12008', '2013-01-07'),
('0891964', 'Ruby', 'Snow', '29773-16346', '2011-12-18'),
('0827171', 'Arya', 'Campos', '18120-95884', '2013-01-21'),
('0886733', 'Yaretzi', 'Joseph', '88344-15559', '2010-10-21'),
('0885275', 'Leah', 'Roman', '89744-83342', '2010-01-03'),
('0837712', 'Corinne', 'Huffman', '66587-05136', '2011-04-03'),
('0803756', 'Enrique', 'Tyson', '41401-89947', '2012-12-30'),
('0832106', 'Holly', 'Kinney', '96961-48255', '2011-11-04'),
('0871899', 'Jaxton', 'Huffman', '58680-81239', '2010-12-24'),
('0834667', 'Leland', 'Mullen', '49088-99950', '2013-08-23'),
('0848042', 'Quincy', 'Merritt', '13619-35297', '2010-11-07'),
('0882325', 'Zara', 'Alford', '40831-15016', '2010-10-15'),
('0891031', 'Samantha', 'Jackson', '24874-47775', '2012-09-13'),
('0846211', 'Jaxen', 'James', '98978-28953', '2012-11-13'),
('0853161', 'Jonas', 'Rivas', '47853-96551', '2012-03-10'),
('0819175', 'Jaidyn', 'Clemons', '76470-06519', '2012-08-27'),
('0839441', 'Ayleen', 'Lee', '29551-94776', '2010-12-19'),
('0857631', 'Gauge', 'Stewart', '98473-70192', '2011-11-23'),
('0857382', 'Bruce', 'Carey', '70786-61873', '2010-01-01'),
('0816380', 'Amani', 'Love', '25672-23781', '2010-12-09'),
('0808692', 'Taraji', 'Mills', '80141-75446', '2012-02-03'),
('0875869', 'Keira', 'Byrd', '33465-41147', '2013-03-27'),
('0800811', 'Deegan', 'Strong', '57118-37389', '2011-07-05'),
('0838774', 'Marvin', 'Mcmahon', '39361-84625', '2010-09-14'),
('0848415', 'Lindsay', 'Walker', '90980-43462', '2011-06-28'),
('0840712', 'Alden', 'Ware', '11362-49381', '2013-03-19'),
('0889517', 'Heaven', 'Brady', '04490-83342', '2010-05-13'),
('0804162', 'Arabella', 'Villarreal', '19814-98358', '2011-02-26'),
('0870247', 'Stephen', 'Roy', '74814-12661', '2011-10-11'),
('0823855', 'Lukas', 'Ortega', '53365-11487', '2010-10-14'),
('0885385', 'Rylie', 'Gill', '59235-16274', '2013-01-21'),
('0852696', 'Keira', 'Owens', '40390-46701', '2011-04-29'),
('0882301', 'Steven', 'Thompson', '50374-80378', '2012-09-15'),
('0835161', 'Shelby', 'Buck', '71238-52210', '2011-04-14'),
('0822564', 'Collin', 'Santos', '53223-20765', '2012-01-06'),
('0821986', 'Zain', 'Nelson', '36805-56788', '2013-10-28'),
('0812982', 'Juliana', 'Ewing', '40267-88898', '2010-11-14'),
('0843262', 'Madison', 'Malone', '39761-44632', '2013-11-25'),
('0805577', 'Aria', 'Serrano', '90238-09850', '2010-03-24'),
('0845288', 'Willa', 'Vance', '02587-13960', '2013-03-23'),
('0859163', 'Nathanael', 'Hood', '63909-93284', '2013-08-06'),
('0810022', 'Jaidyn', 'Bond', '99102-95164', '2012-03-03'),
('0857370', 'Milo', 'Pollard', '80725-49081', '2010-10-12');
Insert Into course Values
('CRS01', 'Programming Basics', '3'),
('CRS02', 'OO Programming', '2'),
('CRS03', 'Database Design', '3'),
('CRS04', 'Software Testing', '3'),
('CRS05', 'Modelling', '3'),
('CRS06', 'Web Programming', '3'),
('CRS07', 'Requirement Engineering', '4'),
('CRS08', 'Operating Systems', '2'),
('CRS09', 'Hardware Design', '3'),
('PRJ01', 'Game Development', '4'),
('PRJ02', 'Data Analysis Applications', '4'),
('PRJ03', 'Simulators', '2'),
('PRJ04', 'Mobile Apps', '3'),
('PRJ05', 'Parallel Programming', '3'),
('PRJ06', 'Advanced Technologies', '3');
Insert Into has_completed Values
('0871899', 'BSC', 'Informatica'),
('0834667', 'BSC', 'Informatica'),
('0848042', 'BSC', 'Informatica'),
('0882325', 'BSC', 'Informatica'),
('0891031', 'BSC', 'Informatica'),
('0846211', 'MSC', 'Informatica'),
('0819175', 'MSC', 'Informatica'),
('0839441', 'MSC', 'Informatica'),
('0857631', 'MSC', 'Informatica'),
('0857382', 'MSC', 'Informatica'),
('0816380', 'MSC', 'Informatica'),
('0808692', 'MSC', 'Informatica'),
('0875869', 'MSC', 'Informatica'),
('0800811', 'BSC', 'Informatica'),
('0838774', 'BSC', 'Informatica'),
('0848415', 'BSC', 'Informatica'),
('0840712', 'MSC', 'Informatica'),
('0889517', 'MSC', 'Informatica'),
('0804162', 'MSC', 'Informatica');
Insert Into aregiven Values
('0817347', 'CRS01', 'HMW54'),
('0817347', 'CRS01', 'HMW44'),
('0817347', 'CRS01', 'HMW41'),
('0817347', 'CRS01', 'HMW37'),
('0820356', 'CRS05', 'HMW03'),
('0820356', 'CRS05', 'HMW66'),
('0820356', 'CRS05', 'HMW99'),
('0820356', 'CRS05', 'HMW39'),
('0827171', 'PRJ01', 'HMW04'),
('0827171', 'PRJ01', 'HMW23'),
('0827171', 'PRJ01', 'HMW59'),
('0886733', 'PRJ02', 'HMW59'),
('0837712', 'PRJ04', 'HMW55'),
('0837712', 'PRJ04', 'HMW78'),
('0837712', 'PRJ04', 'HMW77'),
('0837712', 'PRJ04', 'HMW16'),
('0837712', 'PRJ04', 'HMW62'),
('0837712', 'PRJ04', 'HMW13'),
('0837712', 'PRJ04', 'HMW34'),
('0837712', 'PRJ04', 'HMW74'),
('0871899', 'CRS01', 'HMW54'),
('0871899', 'CRS01', 'HMW44'),
('0871899', 'CRS01', 'HMW41'),
('0871899', 'CRS01', 'HMW37'),
('0891031', 'CRS05', 'HMW03'),
('0891031', 'CRS05', 'HMW66'),
('0891031', 'CRS05', 'HMW99'),
('0891031', 'CRS05', 'HMW39'),
('0857631', 'PRJ01', 'HMW04'),
('0857631', 'PRJ01', 'HMW23'),
('0857631', 'PRJ01', 'HMW59'),
('0857382', 'PRJ02', 'HMW59'),
('0808692', 'PRJ04', 'HMW55'),
('0808692', 'PRJ04', 'HMW78'),
('0808692', 'PRJ04', 'HMW77'),
('0808692', 'PRJ04', 'HMW16'),
('0808692', 'PRJ04', 'HMW62'),
('0808692', 'PRJ04', 'HMW13'),
('0808692', 'PRJ04', 'HMW34'),
('0808692', 'PRJ04', 'HMW74'),
('0838774', 'CRS01', 'HMW54'),
('0838774', 'CRS01', 'HMW44'),
('0838774', 'CRS01', 'HMW41'),
('0838774', 'CRS01', 'HMW37'),
('0804162', 'CRS05', 'HMW03'),
('0804162', 'CRS05', 'HMW66'),
('0804162', 'CRS05', 'HMW99'),
('0804162', 'CRS05', 'HMW39'),
('0882301', 'PRJ01', 'HMW04'),
('0882301', 'PRJ01', 'HMW23'),
('0882301', 'PRJ01', 'HMW59'),
('0835161', 'PRJ02', 'HMW59'),
('0821986', 'PRJ04', 'HMW55'),
('0821986', 'PRJ04', 'HMW78'),
('0821986', 'PRJ04', 'HMW77'),
('0821986', 'PRJ04', 'HMW16'),
('0821986', 'PRJ04', 'HMW62'),
('0821986', 'PRJ04', 'HMW13'),
('0821986', 'PRJ04', 'HMW34'),
('0821986', 'PRJ04', 'HMW74'),
('0805577', 'CRS01', 'HMW54'),
('0805577', 'CRS01', 'HMW44'),
('0805577', 'CRS01', 'HMW41'),
('0805577', 'CRS01', 'HMW37'),
('0857370', 'CRS05', 'HMW03'),
('0857370', 'CRS05', 'HMW66'),
('0857370', 'CRS05', 'HMW99'),
('0857370', 'CRS05', 'HMW39');
答案 0 :(得分:0)
你没有得到任何结果,因为你正在使用JOIN
,你需要使用LEFT JOIN
,即使连接表中没有匹配的行,它仍会返回结果。
尝试此查询(它是MySQL,但我认为应该适用于PostgreSQL):
Select s.first_name, s.surname, SUM(c.points)
from student s
join has_completed h
on h.studentid = s.id
left join aregiven a
on a.studentid = s.id
left join course c
on c.code = a.coursecode
group by s.first_name, s.surname;
请注意,如果您有两名同名学生,则应该按s.id
分组。