我有以下数据库与表,我尝试查询它们: 检索已签出超过五本书的所有借方的签名的书籍的名称,地址和数量。 我创建了查询
select name, Address
from BORROWER
where BOOK_LOANS.BookId >= 5;
似乎我需要加入桌子,我被困在这里,我可以请求帮助吗?
CREATE TABLE BOOK (
BookId CHAR(20) NOT NULL,
Title VARCHAR(30) NOT NULL,
PublisherName VARCHAR(20),
PRIMARY KEY (BookId),
FOREIGN KEY (PublisherName) REFERENCES PUBLISHER (PublisherName)
);
CREATE TABLE BOOK_AUTHORS (
BookId CHAR(20) NOT NULL,
AuthorName VARCHAR(30) NOT NULL,
PRIMARY KEY (BookId, AuthorName),
FOREIGN KEY (BookId) REFERENCES BOOK (BookId)
ON DELETE CASCADE
);
// PublisherName was added, as it was referenced by another
// table but did not exist as an attribute.
CREATE TABLE PUBLISHER (
PublisherName VARCHAR(20) NOT NULL,
Address VARCHAR(40) NOT NULL,
Phone CHAR(12),
PRIMARY KEY (PublisherName)
);
CREATE TABLE BOOK_COPIES (
BookId CHAR(20) NOT NULL,
BranchId INTEGER NOT NULL,
No_Of_Copies INTEGER NOT NULL,
PRIMARY KEY (BookId, BranchId),
FOREIGN KEY (BookId) REFERENCES BOOK (BookId)
ON DELETE CASCADE,
FOREIGN KEY (BranchId) REFERENCES BRANCH (BranchId)
ON DELETE CASCADE
);
CREATE TABLE BORROWER (
CardNo INTEGER NOT NULL,
Name VARCHAR(30) NOT NULL,
Address VARCHAR(40) NOT NULL,
Phone CHAR(12),
PRIMARY KEY (CardNo)
);
CREATE TABLE BOOK_LOANS (
CardNo INTEGER NOT NULL,
BookId CHAR(20) NOT NULL,
BranchId INTEGER NOT NULL,
DateOut DATE NOT NULL,
DueDate DATE NOT NULL,
PRIMARY KEY (CardNo, BookId, BranchId),
FOREIGN KEY (CardNo) REFERENCES BORROWER (CardNo)
ON DELETE CASCADE,
FOREIGN KEY (BranchId) REFERENCES BRANCH (BranchId)
ON DELETE CASCADE,
FOREIGN KEY (BookId) REFERENCES BOOK (BookId)
ON DELETE SET NULL
);
CREATE TABLE BRANCH (
BranchId INTEGER NOT NULL,
BranchName VARCHAR(20) NOT NULL,
Address VARCHAR(40) NOT NULL,
PRIMARY KEY (BranchId)
);
答案 0 :(得分:1)
您确实需要一个将BORROWER
表与BOOK_LOANS
表连接起来的连接。连接应该在共享密钥(CardNO
)上。
要计算借阅的图书数量,请使用COUNT()
聚合函数,然后将其与GROUP BY
子句相结合,该子句根据{{1}的唯一组合将数据划分为组}。
现在你已经为每张CardNo借了一些书。
最后,您应用CardNo, Name, Address
子句来过滤群组,只包括那些借用了五本或更多书籍的群组。
HAVING