CREATE TABLE `Students` (
`id` int(11) NOT NULL,
`name` varchar(500) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Students` (`id`, `name`) VALUES
(1, 'Student 1'),
(2, 'Student 2'),
(3, 'Student 3');
CREATE TABLE `lessons` (
`id` int(11) NOT NULL,
`name` varchar(500) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `lessons` (`id`, `name`) VALUES
(1, 'Lesson 1'),
(2, 'Lesson 2'),
(3, 'Lesson 3');
CREATE TABLE `completed` (
`student` int(11) NOT NULL,
`lesson` varchar(500) NOT NULL,
`completed` int(11) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `completed` (`student`, `lesson`, `completed`) VALUES
(1, 2, 1),
(3, 3, 1),
(2, 1, 1);
我们将完成课程的学生添加到“已完成”的表格中。我们需要在“完成”表格中找到5个不存在的课程的学生。
示例输出是;
1, 1
1, 3
2, 2
2, 3
3, 1
3, 2
谢谢
答案 0 :(得分:0)
以下内容应显示所有没有完成课程的学生,这是我认为你要求的:
SELECT Students.id, Students.name FROM Students LEFT JOIN completed
ON Students.id = completed.student WHERE completed.student IS NULL;
答案 1 :(得分:0)
您可以使用NOT EXISTS
SELECT s.Id, s.Name
FROM Students s
WHERE NOT EXISTS (
SELECT 1
FROM Completed
WHERE s.Id = student
)