哪个运算符最适合用于解决此查询

时间:2017-10-02 17:43:10

标签: sql oracle

我正在尝试解决这个问题而且我不确定要使用哪个运算符。我在考虑使用嵌套查询。

这是一个问题:找到已经借用了当前或过去的两本书“XML和XQuery”和“XQuery:XML查询语言”的成员的姓氏和名字。

这是我的代码:

g, h = e.next
  #=> [{"ctime"=>1506127874782, "etime"=>1506149474782, "queue"=>"low"},
  #    {["low", 4]=>1}]
g #=> {"ctime"=>1506127874782, "etime"=>1506149474782, "queue"=>"low"}
h #=> {["low", 4]=>1}
f = g["queue"]
  #=> "low"
diff = g["etime"]-g["ctime"]
  #=> 1506149474782 - 1506127874782 => 21600000
j = range_mins.rindex { |mn| mn <= diff }
  #=> 4
k = [f, j]
  #=> ["low", 4]
h[k] += 1
  #=> 2
h #=> {["low", 4]=>2}

这是我的问题:

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, 'children');
INSERT INTO Book VALUES (15, 5552314569, 'Green Apple', 'Theo Brown', 1978, 'children');
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, 'Lonely Mens', 'Geen Brown', 2014, 'refrence');
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, 1212121212, 'The Giving Tree', 'Shel Silverstein', 1964, 'fiction');
INSERT INTO BOOK VALUES (23, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO BOOK VALUES (24, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
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 (001, 20, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (002, 19, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (005, 17, '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, 25, '02-OCT-17', '25-DEC-17');

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

3 个答案:

答案 0 :(得分:1)

查看此查询是否适合您。

select m.firstname, m.lastname
from (select memberID, bookID from CurrentLoan union all select memberID, bookID from History) a
join Member m on m.memberID = a.memberID
join Book b on b.bookID = a.bookID
where b.title IN ('XML and XQuery', 'XQuery: The XML Query Language');

答案 1 :(得分:1)

您可以UNION HistoryCurrentLoan执行INTERSECT,然后使用 SELECT m.lastname, m.firstname FROM ( SELECT distinct memberID ,bookID FROM CurrentLoan UNION ALL SELECT distinct memberID ,bookID FROM History ) t JOIN BOOK b ON t.bookid = b.bookid AND b.TITLE = 'Crow' JOIN Member m ON m.memberID = t.memberID INTERSECT SELECT m.lastname, m.firstname FROM ( SELECT distinct memberID ,bookID FROM CurrentLoan UNION ALL SELECT distinct memberID ,bookID FROM History ) t JOIN BOOK b ON t.bookid = b.bookid AND b.TITLE = 'Lonely Mens' JOIN Member m ON m.memberID = t.memberID

<?php
function sort_subnets($x, $y){
    $x_arr = explode('.', $x);
    $y_arr = explode('.', $y);
    foreach (range(0, 3) as $i) {
        if ($x_arr[$i] < $y_arr[$i]) {
            return -1;
        } elseif ($x_arr[$i] > $y_arr[$i]) {
            return 1;
        }
    }
    return -1;
}

$subnet_list =
    array('192.169.12',
        '192.167.11',
        '192.169.14',
        '192.168.13',
        '192.167.12',
        '122.169.15',
        '192.167.16'
    );
usort($subnet_list, 'sort_subnets');
print_r($subnet_list);
?>

答案 2 :(得分:1)

另一个解决方案是使用GROUP BYHAVING来查找那些已经购买了两本具有预期名称的不同图书的用户。

SELECT m.lastname, m.firstname
FROM
(
    SELECT distinct memberID ,bookID FROM CurrentLoan 
    UNION ALL
    SELECT distinct memberID ,bookID FROM History
) t 
JOIN BOOK b ON t.bookid = b.bookid AND b.TITLE IN ('Lonely Mens', 'Crow')
JOIN Member m ON m.memberID = t.memberID
GROUP BY t.memberID, m.lastname, m.firstname
HAVING count(distinct t.bookid) = 2