I have two tables, in one to many relationship (Quizzes, Comments): a Quiz can have multiple Comments
I need to display inside a page the last 5 comments for each quiz.
Is it possible to extract theses comments using one single query (if no, which is the best way to do it)? Right now I am executing a separate query for each quiz to extract the last 5 comments.
(my hope is to find a single HQL to allow me extracting theses comments)
ps. I am using hibernate/jpa/mysql
I wrote a complicated SQL that runs with MySQL 😉
the basic idee is:
Precondition is that a comment with a newer
datehas a higheridUpdate: changed SQL. (realized some missing test cases in my small set of test data)
This part of the SQL is later used twice… you should create a View with that.
–
alternative where clause:
abs(max_c.LastEntry_id - ranked_c.Rank) < 5–> Solution using a View: