问题是
对于每位作者,请返回其第一本书的标题和出版日期。结果集应包含五列:作者的ID,作者的姓名,书的ID,书的标题和书的发布日期。
除了要显示每位作者的每本书以及必填项之外,我还有其他想要的输出列,如何获得只显示最早发布日期的书呢?
SELECT authors.id, authors.author, books.id, books.title, books.published_date
FROM authors
INNER JOIN books
ON authors.id = books.author_id
答案 0 :(得分:0)
解决此问题的一种方法是使用一个查询,该查询为每个作者从表册中获取最小发布日期,然后将其联接到2个表中:
select
a.id, a.author,
b.id, b.title, b.published_date
from authors a
inner join books b on b.author_id = a.id
inner join (
select author_id, min(published_date) published_date
from books
group by author_id
) g on g.author_id = b.author_id and g.published_date = b.published_date
答案 1 :(得分:0)
如果使用最新版本的sqlite(3.25或更高版本),则可以使用window functions作为一种干净的方法:
给出此示例数据库:
CREATE TABLE authors(id INTEGER PRIMARY KEY, author TEXT);
INSERT INTO authors VALUES (1, 'John Steinbeck'), (2, 'William Faulkner');
CREATE TABLE books(id INTEGER PRIMARY KEY
, author_id INTEGER REFERENCES authors(id)
, title TEXT, published_date TEXT);
INSERT INTO books(author_id, title, published_date) VALUES
(1, 'The Grapes Of Wrath', '1939-04-14'),
(2, 'As I Lay Dying', '1930-10-06'),
(2, 'The Sound And The Fury', '1929-10-07'),
(1, 'East Of Eden', '1952-09-19'),
(1, 'Tortilla Flat', '1935-01-01');
此查询:
WITH ranked(author_id, author, book_id, title, published_date, published_order) AS
(SELECT a.id, a.author, b.id, b.title, b.published_date
, rank() OVER (PARTITION BY a.id ORDER BY b.published_date)
FROM authors AS a
JOIN books AS b ON a.id = b.author_id)
SELECT author_id, author, book_id, title, published_date
FROM ranked
WHERE published_order = 1
ORDER BY author_id;
将产生:
author_id author book_id title published_date
---------- -------------- ---------- ------------- --------------
1 John Steinbeck 5 Tortilla Flat 1935-01-01
2 William Faulkn 3 The Sound And 1929-10-07
对于每位作者,它会按出版顺序为每本书分配一个等级(在CTE中计算为rank() OVER (PARTITION BY a.id ORDER BY b.published_date)
列),然后仅选择排名为1的图书作为最终结果。