Mybatis Result Maps grouping 3 results -- one-to-multiple limit and mybatis3 --
Reprinted Please note:TheViper http://www.cnblogs.com/TheViper
In the previous two articles, the results of multiple tables are extracted first, and then automatically grouped using mybatis to implement one-to-many result sets. However, in the actual development process, the Comment page is required, for example, using limit in mysql.
SELECT articlecomment.articlecomment_id,articlecomment.commenttime,articlecomment.commentcontent,articlecomment.id as commentuser_id,articlereply.articlereply_id,articlereply.replytime,articlereply.replycontent,articlereply.id as replyuser_id,u1.name AS comment_user,u2.name AS reply_user FROM articlecomment LEFT JOIN articlereply ON articlereply.comment_id=articlecomment.articlecomment_id INNER JOIN USER AS u1 ON u1.id=articlecomment.id INNER JOIN USER AS u2 ON u2.id=articlereply.id ORDER BY commenttime DESC,replytime DESC
You can see that there are three replies to the comments with the id of 1 and 2. In this case, limit is directly used for the comments page. For example, limit 2, the comments with the id of 1 cannot be obtained, after mybatis is automatically grouped, there is only one comment.
The solution is that limit is not used outside. in the where condition, use in subquery for articlecomment_id and limit in the in subquery, retrieve the articlecomment_id of the comment that meets the condition that the specified limit does not have an external connection.
Table Data
SELECT articlecomment.articlecomment_id,articlecomment.commenttime,articlecomment.commentcontent,articlecomment.id AS commentuser_id ,articlereply.articlereply_id,articlereply.replytime,articlereply.replycontent,articlereply.id AS replyuser_id ,u1.name AS comment_user,u2.name AS reply_user FROM articlecomment LEFT JOIN articlereply ON articlereply.comment_id=articlecomment.articlecomment_id INNER JOIN USER AS u1 ON u1.id=articlecomment.id LEFT JOIN USER AS u2 ON u2.id=articlereply.id WHERE articlecomment.articlecomment_id IN(SELECT articlecomment1.articlecomment_id FROM articlecomment AS articlecomment1 WHERE articlecomment1.article_id=1 ORDER BY articlecomment1.articlecomment_id DESC LIMIT 2 ) ORDER BY articlecomment.articlecomment_id DESC,replytime DESC
This version of MySQL doesn't yet support 'limit & IN/ALL/ANY/SOME subquery' is returned '.
It is easy to find this MySQL of laruence, which is also depressing (about using limit in subqueries ). Add a layer to the outside.
SELECT articlecomment.articlecomment_id,articlecomment.commenttime,articlecomment.commentcontent,articlecomment.id AS commentuser_id ,articlereply.articlereply_id,articlereply.replytime,articlereply.replycontent,articlereply.id AS replyuser_id ,u1.name AS comment_user,u2.name AS reply_user FROM articlecomment LEFT JOIN articlereply ON articlereply.comment_id=articlecomment.articlecomment_id INNER JOIN USER AS u1 ON u1.id=articlecomment.id LEFT JOIN USER AS u2 ON u2.id=articlereply.id WHERE articlecomment.articlecomment_id IN(SELECT articlecomment1.articlecomment_id FROM (SELECT * FROM articlecomment AS articlecomment2 WHERE articlecomment2.article_id=1 ORDER BY articlecomment2.articlecomment_id DESC LIMIT 2) AS articlecomment1 ) ORDER BY articlecomment.articlecomment_id DESC,replytime DESC