简体   繁体   English

sql仅返回每个id的最大日期

[英]sql return only max date for each id

I have a database with one table that looks like this 我有一个带有一个看起来像这样的表的数据库

Books(id, title, author_id, date)

I am using type DATE for the date field. 我在日期字段中使用DATE类型。

What the query is looking to do is return only the most recent book from each author, so even for the books that have the same author_id, only return the book that has the most recent date field for that author. 查询要执行的操作是仅返回每个作者的最新书籍,因此,即使对于具有相同author_id的书籍,也仅返回具有该作者的最新日期字段的书籍。 So for all books find the most recent book from each author, returning title, author_id, and date. 因此,对于所有书籍,请查找每位作者的最新书籍,并返回书名,author_id和日期。

I believe I will need to use the MAX function with a GROUP BY author_id....or perhaps include a HAVING max(date)? 我相信我将需要将MAX函数与GROUP BY author_id一起使用。...或者可能包含HAVING max(date)? Sort of a side note is it possible when returning the date to have it be age in years it has been released up to 1 decimal point? 在返回日期时,是否可以将年龄(以年为单位)释放到小数点后1位,这可能是一个补充说明吗? (ex. 3.1 if book date was just over 3 years old?). (例如3.1,如果预订日期刚刚超过3年?)。

You can use this query: 您可以使用以下查询:

SELECT *
FROM Books
WHERE (author_id, date) IN (SELECT author_id, MAX(date)
                            FROM Books
                            GROUP BY author_id)

subquery will return, for every author, the maximum date of a book. 子查询将为每个作者返回一本书的最大日期。 Outer query will return all books for every author with the maximum date. 外部查询将返回具有最长日期的每位作者的所有书籍。

Please notice that this could return more than one book for every author if they are published in the same maximum date. 请注意,如果同一作者在相同的最大发布日期出版,那么这可能为每位作者返回多本书籍。

SELECT b1.*
FROM Books b1
JOIN (
    SELECT author_id, MAX(date) date
    FROM Books
    GROUP BY author_id
) b2
  ON b2.author_id = b1.author_id
 AND b2.date = b1.date

demo 演示

Use this:- 用这个:-

SELECT id,title,author_id,date
FROM Books t1
WHERE t1.id = (SELECT t2.id
              FROM Books t2
              WHERE t2.author_id = t1.author_id
              ORDER BY t2.date desc
              LIMIT 1)

This query will return the most recent book of each author. 该查询将返回每个作者的最新书籍。

Check this:- SQL FIDDLE 检查此: -SQL FIDDLE

Query: 查询:

SELECT b.*
FROM Books b
WHERE b.id = (SELECT b2.id
              FROM Books b2
              WHERE b2.author_id = b.author_id
              ORDER BY b2.date desc
              LIMIT 1)

On SQL-Server, I would write something like this to find the most recent date for each author: 在SQL Server上,我将编写以下内容以查找每个作者的最新日期:

Select author_id, Max(date) From Books Group By author_id

You can then use a modification of this as a subquery to find the most recent book for each author: 然后,您可以使用对此的修改作为子查询来查找每位作者的最新书籍:

Select B.* from Books as B
where B.date = (Select Max(date) From Books B2 where B2.author_id = B.author_id)

For MySQL, I'm not sure if this will work but probably that it will. 对于MySQL,我不确定这是否会起作用,但可能会。 Of course, there are other ways of doing this. 当然,还有其他方法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM