SOS: How does MySQL find results based on the given ID sequence?

Source: Internet
Author: User
Tags id3 repetition
Dear God, now I have a difficult problem that needs your help.

There are two of the following tables
Post table: Article table.
Record table: A table of records used to record the user's reading order.

The record table records the primary key ID of the post table with a field PostID, based on the user's reading order, and the Record.postid foreign key is post.id.

Now you need to find the Post table article based on the PostID in the record table. (assuming that the reading order is: ' Article 1, article 2, article 2, article 3, Article 3 ' allows the article to repeat. )
I started with

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

Finding results can only look for postid1,postid2,postid3 articles.

The business logic you want now is:
1: Find post table articles based on user's reading history.
2: Allow repetition.

For example, users read the article reading order is: ' Article 1, article 2, article 2, article 3, Article 3 ', look for the post table results are: POST.ID1, post.id2,post.id2,post.id2,post.id3.

Note: Consider using a For loop

 for (.....){      ...      变量 数组x;      数组x添加 SELECT * FROM post WHERE id = postidx;      .... }

But they worry about efficiency.

Reply content:

Dear God, now I have a difficult problem that needs your help.

There are two of the following tables
Post table: Article table.
Record table: A table of records used to record the user's reading order.

The record table records the primary key ID of the post table with a field PostID, based on the user's reading order, and the Record.postid foreign key is post.id.

Now you need to find the Post table article based on the PostID in the record table. (assuming that the reading order is: ' Article 1, article 2, article 2, article 3, Article 3 ' allows the article to repeat. )
I started with

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

Finding results can only look for postid1,postid2,postid3 articles.

The business logic you want now is:
1: Find post table articles based on user's reading history.
2: Allow repetition.

For example, users read the article reading order is: ' Article 1, article 2, article 2, article 3, Article 3 ', look for the post table results are: POST.ID1, post.id2,post.id2,post.id2,post.id3.

Note: Consider using a For loop

 for (.....){      ...      变量 数组x;      数组x添加 SELECT * FROM post WHERE id = postidx;      .... }

But they worry about efficiency.

SELECT p.*
From Post P
Left JOIN Record R on r.postid = p.id
ORDER by R.id ASC

The record table records the reading order, so it needs to be sorted according to the order of the record table

can provide a way of thinking: using in to extract data from the database, map to establish a corresponding relationship map.put (postid,post). The data types required for assembly are then taken out in the order of the IDs

  • Related Article

    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.