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:
books
book_id
book_title
users
user_id
user_name
book_reviews
review_id
book_id
user_id
review_date (unix timestamp date)
I would like to query 30 of the latest book reviews. 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. 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.
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.
Thanks a ton.