select 所有成员使用 NOT EXISTS
Usage of NOT EXISTS to select ALL members
我正在尝试使用 MySQL 为图书馆数据库编写 SQL 查询。
有两个 tables shelf(studentnumber, booknumber)
和 booklist(booknumber,booktitle,language)
。书单 table 条目中有 4 种不同的语言,即 italian, spanish, hungarian, german
.
我想学 studentnumbers
已阅读所选语言所有书籍的
tables 的示例数据:
create table shelf(studentnumber INT, booknumber INT);
INSERT INTO shelf values(1,1),(1,2),(1,3),(2,1),(2,3)(2,4),(2,5),
(2,6),(3,6),(3,7)(3,8),(3,9);
create table booklist(booknumber INT, booktitle VARCHAR(50), language VARCHAR(10);
INSERT INTO booklist values(1, 'FirstBook', 'italian'),(2,'SecondBook', 'spanish'),
(3,'ThirdBook','italian'),(4,'FourthBook','german'),(5,'FifthBook','german'),
(6, 'SixthBook','spanish'),(7,'SeventhBook','hungarian'),(8,'EightBook','hungarian'),
(9,'NinthBook','hungarian'),(10,'TenthBook','Spanish'),(11,'EleventhBook', 'italian');
示例输出:
当您查看书架和书单 tables 时,您会看到学生编号为 2 的学生阅读了所有德语书籍,学生编号为 3 的学生阅读了所有匈牙利语书籍。
但是没有学生读完所有意大利语或西班牙语的书。
代码的最后一部分如下所示,但我无法构建第一部分,可能会包含 NOT EXISTS
SELECT booknumber FROM booklist WHERE language='italian';
这应该很适合你:
select studentnumber
from shelf s1 join booklist b1 on s1.booknumber=b1.booknumber
group by studentNumber, language
having count(language) >= (
select count(*) from booklist b2
where b1.language=b2.language
group by b2.language)
输出:
2
3
您还可以添加第一个 select 语句语言和计数 (*),这将为您提供更多信息(他们阅读的语言以及书籍的数量)即
select studentnumber,language,count(*)
//rest of code
输出
2, german, 2
3, hungarian, 3
更新
要显示所有阅读过指定语言书籍的学生(按照评论中的要求),只需添加一个 WHERE 子句:
select studentnumber
from shelf s1 join booklist b1 on s1.booknumber=b1.booknumber
where language='german'
group by studentNumber, language
having count(language) >= (
select count(*) from booklist b2
where b1.language=b2.language
group by b2.language)
输出:
2
我正在尝试使用 MySQL 为图书馆数据库编写 SQL 查询。
有两个 tables shelf(studentnumber, booknumber)
和 booklist(booknumber,booktitle,language)
。书单 table 条目中有 4 种不同的语言,即 italian, spanish, hungarian, german
.
我想学 studentnumbers
已阅读所选语言所有书籍的
tables 的示例数据:
create table shelf(studentnumber INT, booknumber INT);
INSERT INTO shelf values(1,1),(1,2),(1,3),(2,1),(2,3)(2,4),(2,5),
(2,6),(3,6),(3,7)(3,8),(3,9);
create table booklist(booknumber INT, booktitle VARCHAR(50), language VARCHAR(10);
INSERT INTO booklist values(1, 'FirstBook', 'italian'),(2,'SecondBook', 'spanish'),
(3,'ThirdBook','italian'),(4,'FourthBook','german'),(5,'FifthBook','german'),
(6, 'SixthBook','spanish'),(7,'SeventhBook','hungarian'),(8,'EightBook','hungarian'),
(9,'NinthBook','hungarian'),(10,'TenthBook','Spanish'),(11,'EleventhBook', 'italian');
示例输出: 当您查看书架和书单 tables 时,您会看到学生编号为 2 的学生阅读了所有德语书籍,学生编号为 3 的学生阅读了所有匈牙利语书籍。 但是没有学生读完所有意大利语或西班牙语的书。
代码的最后一部分如下所示,但我无法构建第一部分,可能会包含 NOT EXISTS
SELECT booknumber FROM booklist WHERE language='italian';
这应该很适合你:
select studentnumber
from shelf s1 join booklist b1 on s1.booknumber=b1.booknumber
group by studentNumber, language
having count(language) >= (
select count(*) from booklist b2
where b1.language=b2.language
group by b2.language)
输出:
2
3
您还可以添加第一个 select 语句语言和计数 (*),这将为您提供更多信息(他们阅读的语言以及书籍的数量)即
select studentnumber,language,count(*)
//rest of code
输出
2, german, 2
3, hungarian, 3
更新
要显示所有阅读过指定语言书籍的学生(按照评论中的要求),只需添加一个 WHERE 子句:
select studentnumber
from shelf s1 join booklist b1 on s1.booknumber=b1.booknumber
where language='german'
group by studentNumber, language
having count(language) >= (
select count(*) from booklist b2
where b1.language=b2.language
group by b2.language)
输出:
2