我遇到了一些问题

时间:2017-10-04 01:40:16

标签: sql oracle

问题是: 找到当前或过去都借用过标题为“XML和XQuery”和“XQuery:XML查询语言”的书籍的成员的姓氏和名字。

对于我的查询,它应该打印出#34; David Jones"只是因为书名都在CurrentLoan和历史中,而且还打印出其他三个成员。我的查询有问题吗?

以下是数据:http://sqlfiddle.com/#!4/6fe0b/1

我的代码:

CREATE TABLE Book
    (bookID INT,
    ISBN INT,
    title varchar (30),
    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, 1113312336, 'The Dog', 'Jack Crow', 1990, 'fiction');
INSERT INTO Book VALUES (12, 2221254896, 'Worms', 'Jim Kan', 2013, 'childrens');
INSERT INTO Book VALUES (13, 3332546987, 'Crow', 'Jan Flo', 2000, 'fiction'); 
INSERT INTO Book VALUES (14, 4443456215, 'Big Dog', 'Lan Big', 1993, 'childrens');
INSERT INTO Book VALUES (15, 5552314569, 'Green Apple', 'Theo Brown', 1978, 'childrens');
INSERT INTO Book VALUES (16, 6664581631, 'Red Bean', 'Khang Nk', 2017, 'fiction');
INSERT INTO Book VALUES (17, 7771452369, 'XML and XQuery', 'Author Le', 2017, 'non-fiction');
INSERT INTO Book VALUES (18, 8881245525, 'The Dark Room', 'Jack Se', 2017, 'fiction');
INSERT INTO Book VALUES (19, 9991123546, 'Yellow Bird', 'Jake Red', 2014, 'reference');
INSERT INTO Book VALUES (20, 1122112356, 'XQuery: The XML Query Language', 'Heart Le', 2002, 'fiction');
INSERT INTO BOOK VALUES (21, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (22, 2121212121, 'Gone Fishing', 'Shel Silverstein', 1964, 'reference');
INSERT INTO BOOK VALUES (23, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'childrens');
INSERT INTO BOOK VALUES (24, 3131313131, 'The Red Bird', 'Jake Red', 2016, 'childrens');
INSERT INTO BOOK VALUES (25, 1414141414, 'The Very Blue Boy', 'Ben Jen', 2006, '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 Member VALUES (007, 'Jones', 'David', '4th Street', 14812365789, 10);

INSERT INTO CurrentLoan VALUES (002, 25, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (001, 20, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (006, 14, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (003, 15, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 18, '13-APR-17', '12-MAY-17');
INSERT INTO CurrentLoan VALUES (007, 17, '02-OCT-17', '25-DEC-17');

INSERT INTO History VALUES (002, 17, '14-Jan-17', '04-JUN-17');
INSERT INTO History VALUES (003, 19, '12-Jan-17', '04-NOV-17');
INSERT INTO History VALUES (001, 13, '14-APR-17', '08-OCT-17');
INSERT INTO History VALUES (006, 20, '14-MAY-17', '04-DEC-17');
INSERT INTO History VALUES (007, 20, '02-OCT-16', '25-DEC-16');
COMMIT;

我的查询:

SELECT lastname, firstname
FROM Member
INNER JOIN CurrentLoan ON Member.memberID = CurrentLoan.memberID
INNER JOIN Book ON CurrentLoan.bookID = Book.bookID
WHERE Book.title = 'XML and XQuery' or title = 'XQuery: The XML Query Language'
UNION
SELECT lastname, firstname
FROM Member
INNER JOIN History ON Member.memberID = History.memberID
INNER JOIN Book ON History.bookID = Book.bookID
WHERE Book.title = 'XML and XQuery' or title = 'XQuery: The XML Query Language';

1 个答案:

答案 0 :(得分:2)

回答这个问题有几个障碍。首先,我们需要处理一个成员当前借书或过去借过它的要求。您有两个表CurrentLoanHistory,其结构几乎相同。出于查询的目的,我们实际上只关心memberIDbookID。合并它们的一种方法是在这两个表之间使用联合。这是有道理的,因为如果一个成员在任一个表中出现一本书,我们都要计算它。

在此过程中,我们现在可以按成员汇总您的表格,并检查已使用两个感兴趣的标题的成员。在下面的t2子查询中,我们找到了匹配的memberID。然后,所需要的只是再一次加入Member表,以检索每个匹配成员的完整记录。

SELECT
    t1.firstname,
    t1.lastname
FROM Member t1
INNER JOIN
(
    SELECT t.memberID
    FROM
    (
        SELECT memberID, bookID
        FROM CurrentLoan
        UNION ALL
        SELECT memberID, bookID
        FROM History
    ) t
    INNER JOIN Member m
        ON t.memberID = m.memberID
    INNER JOIN Book b
        ON t.bookID = b.bookID
    WHERE
        b.title IN ('XML and XQuery', 'XQuery: The XML Query Language')
    GROUP BY t.memberID
    HAVING
        COUNT(DISTINCT b.title) = 2
) t2
    ON t1.memberID = t2.memberID

这只会输出David Jones,正如您所期望的那样。

在这里演示:

SQLFiddle