我不想将CurrentLoan和历史记录结合起来,而是希望当前和历史记录显示在当前和历史记录中由同一成员签出的书籍数量。
问题:
对于每个成员(会员ID,姓氏和名字),列出会员当前签出的书籍数量,以及该会员过去检出的书籍数量。如果会员多次签出同一本书,则会多次计算。如果会员没有结账,则应将其计为0。
代码:
CREATE TABLE Book
(bookID INT,
ISBN INT,
title varchar (60),
author varchar (20),
publish_year INT,
category varchar(20),
PRIMARY KEY (bookID));
CREATE TABLE Member
(memberID INT,
lastname varchar (20),
firstname varchar (20),
address varchar(20),
phone_number INT,
limit_ INT,
PRIMARY KEY (memberID));
CREATE TABLE CurrentLoan
(memberID INT ,
bookID INT,
loan_date DATE,
due_date DATE,
PRIMARY KEY (memberID, bookID),
FOREIGN KEY (memberID) REFERENCES Member(memberID),
FOREIGN KEY (bookID) REFERENCES Book(bookID));
CREATE TABLE History
(memberID INT,
bookID INT,
loan_date DATE,
return_date DATE,
PRIMARY KEY (memberID, bookID, loan_date),
FOREIGN KEY (memberID) REFERENCES Member(memberID),
FOREIGN KEY (bookID) REFERENCES Book(bookID));
INSERT INTO Book VALUES (10, 7771452369, 'XML and XQuery', 'Author Le', 2017, 'reference');
INSERT INTO Book VALUES (11, 8881245525, 'XQuery: The XML Query Language', 'Jack Se', 2017, 'reference');
INSERT INTO Book VALUES (12, 9991123546, 'Yellow Bird', 'Jake Red', 2014, 'reference');
INSERT INTO BOOK VALUES (13, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (14, 2121212121, 'Gone Fishing', 'Shel Silverstein', 1964, 'reference');
INSERT INTO BOOK VALUES (15, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'childrens');
INSERT INTO BOOK VALUES (16, 3131313131, 'The Red Bird', 'Jake Red', 2016, 'childrens');
INSERT INTO BOOK VALUES (17, 1414141414, 'The Very Blue Boy', 'Ben Jen', 2006, 'fiction');
INSERT INTO Book VALUES (18, 1113312336, 'Harry Potter 1', 'J. K. Rowling', 2000, 'fiction');
Insert INTO Book VALUES (19, 1113331142, 'Harry Potter 1', 'J. K. Rowling', 2000, 'fiction');
INSERT INTO Book VALUES (20, 2221257787, 'The Real Harry Potter 2', 'J. K. Rowling', 2009, 'fiction');
INSERT INTO Book VALUES (21, 2221254896, 'The Fake Harry Potter 3', 'J. K. Rowling', 2010, 'fiction');
INSERT INTO Book VALUES (22, 2221254896, 'The Fake Harry Potter 3', 'J. K. Rowling', 2010, 'fiction');
INSERT INTO Book VALUES (23, 2221254896, 'The Fake Harry Potter 4', 'J. K. Rowling', 2012, 'fiction');
INSERT INTO Book VALUES (24, 2221254896, 'The Fake Harry Potter 4', 'J. K. Rowling', 2012, 'fiction');
INSERT INTO Member VALUES (001, 'Lee', 'Nancy', 'Brownlea Drive', 1254896325, 10);
INSERT INTO Member VALUES (002, 'Le', 'Ray', '10th Street', 1234561256, 2);
INSERT INTO Member VALUES (003, 'Kan', 'Charlie', '5th Street', 1234567236, 8);
INSERT INTO Member VALUES (004, 'Brown', 'Joe', 'Elm Street', 1234567845, 9);
INSERT INTO Member VALUES (005, 'Smith', 'John', '33 East', 1234567890, 3);
INSERT INTO Member VALUES (006, 'Khang', 'Nkaujyi', '358 Spencer', 2145345625, 5);
INSERT INTO CurrentLoan VALUES (001, 10, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (001, 11, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (001, 18, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (001, 20, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (001, 22, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (002, 11, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (003, 19, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (004, 18, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (004, 17, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 18, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 20, '12-OCT-17', '09-NOV-17');
INSERT INTO History VALUES (001, 15, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 21, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 18, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 15, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 10, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (003, 23, '12-FEB-16', '05-MAY-16');
INSERT INTO History VALUES (004, 13, '12-JUN-16', '05-AUG-16');
INSERT INTO History VALUES (004, 20, '12-JUN-16', '05-AUG-16');
INSERT INTO History VALUES (004, 21, '12-JUN-16', '05-AUG-16');
INSERT INTO History VALUES (005, 21, '12-JUN-16', '05-AUG-16');
INSERT INTO History VALUES (005, 24, '12-JUN-16', '05-AUG-16');
COMMIT;
查询:
SELECT Member.memberID, lastname, firstname, COUNT (loans.bookID) as numberofbooks
FROM Member
LEFT OUTER JOIN
(
SELECT
memberID, bookID FROM CurrentLoan
union all
SELECT
memberID, bookID FROM History
) loans ON Member.memberID = Loans.memberID
GROUP BY Member.memberID, lastname, firstname
ORDER BY Member.memberID, lastname, firstname;
这就是它的作用,它将两者结合起来:http://sqlfiddle.com/#!4/1a5785/1
答案 0 :(得分:1)
SELECT Member.memberID, lastname, firstname,
case when min(CurrentLoanGrp.cnt) is null then 0 else min(CurrentLoanGrp.cnt) end as Currentnumberofbooks,
case when min(HistoryGrp.cnt) is null then 0 else min(HistoryGrp.cnt) end as Historynumberofbooks
FROM Member
left outer join (select count(bookID) cnt, memberID from CurrentLoan group by memberID) CurrentLoanGrp on CurrentLoanGrp.memberID = Member.memberID
left outer join (select count(bookID) cnt, memberID from History group by memberID) HistoryGrp on HistoryGrp.memberID = Member.memberID
GROUP BY Member.memberID, lastname, firstname
ORDER BY Member.memberID, lastname, firstname;