简体   繁体   English

通过查询获取MySQL组以显示该组中具有最高值的行

[英]Getting a MySQL group by query to display the row in that group with the highest value

I'm trying to figure out how to query my database so that it will essentially first ORDER my results and then GROUP them... This question seems to be slightly common and I have found examples but I still don't quite grasp the 'how' to do this and use the examples in my own situation.... So all help is definitely appreciated. 我正在尝试找出如何查询数据库的方法,这样它实际上将首先对我的结果进行排序,然后对它们进行分组……这个问题似乎有点普遍,我已经找到了一些示例,但我仍然不太了解“如何做到这一点,并在我自己的情况下使用示例。...因此,我们非常感谢所有帮助。

Here are my MySQL tables: 这是我的MySQL表:

books 图书
book_id book_id
book_title 书名

users 用户
user_id 用户身份
user_name 用户名

book_reviews book_reviews
review_id REVIEW_ID
book_id book_id
user_id 用户身份
review_date (unix timestamp date) review_date(unix时间戳日期)

I would like to query 30 of the latest book reviews. 我想查询30本书的最新评论。 They will simply display as: 它们将简单显示为:
Book Name 书名
Username of Reviewer 审稿人的用户名

However I would like to display each book no more than one time. 但是,我希望每本书最多展示一次。 So the review shown in the list should be the most recently added review. 因此,列表中显示的评论应该是最近添加的评论。 To do this I have been simply grouping by book_name and ordering by review_date DESC. 为此,我只是按book_name分组,并按review_date DESC排序。 But querying this way doesn't display the record with the most recently added review_date as the grouped by row so my data is incorrect. 但是以这种方式查询不会显示最近添加的review_date作为按行分组的记录,因此我的数据不正确。

Here is my current query: 这是我目前的查询:

SELECT books.books_title, users.user_name, book_reviews.review_id FROM books, users, book_reviews WHERE book_reviews.book_id = books.book_id AND book_reviews.user_id = users.user_id GROUP BY book_title ORDER BY review_date DESC LIMIT 30

From what I've read it seems like I have to have a subquery where I get the MAX(review_date) value but I still don't understand how to link it all up. 从我阅读的内容来看,似乎必须要有一个子查询,才能获得MAX(review_date)值,但我仍然不知道如何将其全部链接起来。

Thanks a ton. 万分感谢。

Use: 采用:

  SELECT x.book_title,
         x.user_name
    FROM (SELECT b.book_title,
                 u.user_name,
                 br.review_date,
                 CASE
                   WHEN @book = b.book_title THEN @rownum := @rownum + 1
                   ELSE @rownum := 1
                 END AS rank,
                 @book := b.book_title
            FROM BOOKS b
            JOIN BOOK_REVIEWS br ON br.book_id = b.book_id
            JOIN USERS u ON u.user_id = br.user_id
            JOIN (SELECT @rownum := 0, @book := '') r
        ORDER BY b.book_title, br.review_date DESC) x
   WHERE x.rank = 1
ORDER BY x.review_date DESC
   LIMIT 30

MySQL doesn't have analytical/ranking/windowing functionality, but this ranks the reviews where the latest is marked as 1. This is on a per book basis... MySQL没有分析/排名/窗口功能,但是这会将评论的排名标记为1。这是每本书的基础...

I exposed the review date to order by the latest of those which are the latest per book... 我按书中最新的顺序列出了审核日期,以每本书的最新日期为准。

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

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