For help: how does mysql search results based on the given ID sequence?

Source: Internet
Author: User
Dear friends, I have encountered a difficult problem and need your help. There are two post tables: article table. Record table: a record table used to record the reading sequence of users. The record table uses a postid field to record the primary key ID of the post table based on the user's reading order, both record. p.

There are two tables below
Post table: document table.
Record table: a record table used to record the reading sequence of users.

The record table uses a postid field to record the primary key ID of the post table based on the user's reading order. the foreign key of record. postid is post. id.

Now you need to search for post table articles based on postid in the record table. (Assume that the reading sequence is: 'Article 1, Article 2, Article 2, Article 3, and article 3 .)
Start using

SELECT *FROM postWHERE id IN (postid1,postid2,postid2,postid3,postid3);

You can only search for postid1, postid2, and postid3.

The business logic is as follows:
1: Search for post table articles based on the user's reading records.
2: repeat is allowed.

For example, if the reading sequence of an article is 'Article 1, Article 2, Article 2, Article 3, Article 3', the result of searching for the post table is: post. id1, post. id2, post. id2, post. id2, post. id3.

Note: We have considered using the FOR loop.

For (...) {variable array x; array x add SELECT * FROM post WHERE id = postidx ;....}

However, they are worried about efficiency.

Reply content:

Dear friends, I have encountered a difficult problem and need your help.

There are two tables below
Post table: document table.
Record table: a record table used to record the reading sequence of users.

The record table uses a postid field to record the primary key ID of the post table based on the user's reading order. the foreign key of record. postid is post. id.

Now you need to search for post table articles based on postid in the record table. (Assume that the reading sequence is: 'Article 1, Article 2, Article 2, Article 3, and article 3 .)
Start using

SELECT *FROM postWHERE id IN (postid1,postid2,postid2,postid3,postid3);

You can only search for postid1, postid2, and postid3.

The business logic is as follows:
1: Search for post table articles based on the user's reading records.
2: repeat is allowed.

For example, if the reading sequence of an article is 'Article 1, Article 2, Article 2, Article 3, Article 3', the result of searching for the post table is: post. id1, post. id2, post. id2, post. id2, post. id3.

Note: We have considered using the FOR loop.

For (...) {variable array x; array x add SELECT * FROM post WHERE id = postidx ;....}

However, they are worried about efficiency.

Select p .*
FROM Post P
Left join Record r on r. PostID = P. ID
Order by r. ID ASC

The reading sequence of records in the Record table. Therefore, you need to sort the records in the order of the Record table.

One idea can be provided: extract data from the database using the in method, and use map to establish the corresponding map. put (postid, post ). Then the data types are assembled in the order of IDs.

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.