Mybatis Result Maps grouping 3 results -- one-to-multiple limit and mybatis3 --

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.