我正在尝试使用count来解决这个问题。它为作者返回了正确数量的书籍,但根据问题,我不能两次拥有相同的书名。是否有更好的运算符用于此问题?
以下是问题:
查找图书馆拥有最多不同图书的作者姓名(同一本图书的多个副本只计为一本图书)
这是我的代码:
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, 'Yellow Bird', 'Jake Red', 2014, 'refrence');
INSERT INTO Book
VALUES (20, 1122112356, 'XQuery: The XML Query Language', 'Heart Le', 2002, 'fiction');
INSERT INTO Book
VALUES (21, 1212121212, 'Gone Fishing', 'Shel Silverstein', 1964, 'reference');
INSERT INTO Book
VALUES (22, 2121212121, 'Gone Fishing', 'Shel Silverstein', 1964, 'reference');
INSERT INTO Book
VALUES (23, 1313131313, 'The Lazy Dog', 'Jake Red', 2016, 'children');
INSERT INTO Book
VALUES (24, 3131313131, 'The Red Bird', '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, 'Kope', 'NON', '358 Seng', 2145345625, 5);
INSERT INTO Member
VALUES (007, 'Jones', 'David', '4th Street', 14812365789, 10);
INSERT INTO CurrentLoan VALUES (002, 20, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (001, 19, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (006, 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 (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, 12, '02-OCT-16', '25-DEC-16');
COMMIT;
这是我的问题:
SELECT
author, COUNT(*) AS numberofbooks
FROM
Book
GROUP BY
author
ORDER BY
numberofbooks DESC;
答案 0 :(得分:1)
就您的查询而言,它是正确的,请检查作者姓名或拼写差异可能有额外的空间。
答案 1 :(得分:1)
要消除重复的标题,您需要在子查询中生成唯一的行,然后计算这些行,如下所示:
SELECT Author, Count(*) NumberOfTitles
FROM
(
SELECT DISTINCT Author, Title
FROM Book
) Subquery
GROUP BY Author
答案 2 :(得分:1)
我只需在|¯¯¯¯¯¯¯|Arlidio|_______|
之前使用distinct
。
title