让这个查询做它应该做的事情

时间:2017-10-05 13:58:51

标签: sql oracle

所以,我认为这个问题只是希望那个现在或过去已经出书/检查所有书籍的会员都有书名和#34;哈利波特"。如果有一个会员只检查4本书中的当前或历史2,那么它不应该输出该会员,如果该会员只检查4本书中的2本,那么它不应该是输出那个。但是,如果一个成员目前或已查看所有书籍标题"哈利波特"如果所有的书籍目前都已签出或过去,那么它应该是该成员。但是,如果所有的书都是当前或仅在过去检查,它应该输出该成员。

以下是问题:

列出目前或过去借用图书馆中标题为“哈利波特”的所有图书的成员的memberID,名字和姓氏。如果任何此类书籍有多份副本,则该成员必须至少借用每本书籍的一份副本。

代码:

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 (001, 24, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (002, 21, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (002, 23, '14-FEB-17', '12-MAR-17');

INSERT INTO History VALUES (002, 10, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 11, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 12, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 18, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 20, '03-JAN-16', '25-MAY-16');
INSERT INTO History VALUES (002, 13, '03-JAN-16', '25-MAY-16');
COMMIT;

此查询将输出memberID 2,因为该memeber所有的书都标题为" Harry Potter"查看当前或历史。但是,会员ID 1并没有,并且它应该输出它,因为它的所有书都有标题"哈利波特"在当前。

查询:

SELECT Member.memberID, lastname, firstname
FROM Member
INNER JOIN CurrentLoan ON Member.memberID = CurrentLoan.memberID
INNER JOIN Book ON CurrentLoan.bookID = Book.bookID
WHERE Book.title like '%Harry Potter%'
intersect
SELECT Member.memberID, lastname, firstname
FROM Member
INNER JOIN History ON Member.memberID = History.memberID
INNER JOIN Book ON History.bookID = Book.bookID
WHERE Book.title like '%Harry Potter%';

2 个答案:

答案 0 :(得分:1)

INTERSECT只返回两个查询中出现的rwos - 因此您只会获得目前已经获得相应图书的人,并且过去也会搜索相应的图书。

你想要这样的东西:

查询1

WITH matching_books AS (
  SELECT BookID, ISBN
  FROM   book
  WHERE  title LIKE '%Harry Potter%'
)
SELECT MemberId,
       Firstname,
       lastname
FROM   Member
WHERE  MemberID IN (
         SELECT MemberID
         FROM   ( SELECT MemberID, BookID
                  FROM   CurrentLoan
                  UNION ALL
                  SELECT MemberID, BookID
                  FROM   History
                ) l
                INNER JOIN matching_books b
                ON ( l.bookId = b.bookId )
         GROUP BY MemberID
         HAVING COUNT( DISTINCT b.ISBN ) = ( SELECT COUNT( DISTINCT isbn ) FROM matching_books )
       )

<强> Results

没有结果 - 这是因为没有用户为哈利波特书中的每个唯一ISBN取出所有书籍。您可以重做查询以比较标题(而不是ISBN),它会给出您要查找的结果,但不保证这是唯一的。

答案 1 :(得分:1)

您可以使用GROUP BYHAVING来检查“所有”图书。

WITH cte AS (
    SELECT *
    FROM Book
    WHERE  title LIKE '%Harry Potter%'
), cte2 AS (
   SELECT memberId
   FROM (SELECT l.*, b.title
         FROM currentLoan l
         JOIN Book b  ON b.bookId = l.bookId
         UNION ALL
         SELECT l.*, b.title
         FROM History l
         JOIN Book b  ON b.bookId = l.bookId) sub
   WHERE bookID IN (SELECT bookID FROM cte)
   GROUP BY memberId
   HAVING COUNT(DISTINCT(title)) = (SELECT COUNT(DISTINCT title) FROM cte)
)
SELECT memberID, firstname,  lastname
FROM Member
WHERE memberId IN (SELECT memberId FROM cte2);

<强> Rextester Demo

修改

更简单的形式:

SELECT memberID, firstname,  lastname
FROM (
    SELECT  b.title, m.memberID, m.firstname,  m.lastname
    FROM currentLoan l
    JOIN Book b  ON b.bookId = l.bookId
    JOIN member m ON l.memberId = m.memberId
    WHERE b.bookID IN (SELECT bookId FROM Book WHERE title LIKE '%Harry Potter%')
    UNION ALL
    SELECT  b.title, m.memberID, m.firstname,  m.lastname
    FROM History l
    JOIN Book b  ON b.bookId = l.bookId
    JOIN member m ON l.memberId = m.memberId
    WHERE b.bookID IN (SELECT bookId FROM Book WHERE title LIKE '%Harry Potter%')
    ) sub
GROUP BY memberID, firstname,  lastname
HAVING COUNT(DISTINCT(title))= 
      (SELECT COUNT(DISTINCT title) FROM Book WHERE title LIKE '%Harry Potter%');

<强> Rextester Demo2