Home  >  Article  >  Backend Development  >  Help: How does mysql find results based on the given ID order?

Help: How does mysql find results based on the given ID order?

WBOY
WBOYOriginal
2016-10-10 11:56:231104browse

Dear masters, now I have encountered a problem and need your help.

There are the following two tables
post table: article table.
Record table: Record table, used to record the user's reading order.

The record table uses a field postid to record the primary key ID of the post table according to the order in which the user reads articles, that is, the foreign key of record.postid is post.id.

Now we need to find the articles in the post table based on the postid in the record table. (Assume that the reading order is: 'Article 1, Article 2, Article 2, Article 3, Article 3' allows repeated articles.)
I started with

<code>SELECT *
FROM post
WHERE id IN (postid1,postid2,postid2,postid3,postid3);</code>

The search results can only find articles with postid1, postid2, and postid3.

The business logic we want now is:
1: Find articles in the post table based on the user’s reading record.
2: Duplication is allowed.

For example, if the reading order of the articles read by the user is: 'Article 1, Article 2, Article 2, Article 3, Article 3', the result of searching the post table is: post.id1, ​​post.id2, post.id2, post.id2 ,post.id3.

Note: Consider using FOR loop

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

But I am worried about efficiency.

Reply content:

Dear masters, now I have encountered a problem and need your help.

There are the following two tables
post table: article table.
Record table: Record table, used to record the user's reading order.

The record table uses a field postid to record the primary key ID of the post table according to the order in which the user reads articles, that is, the foreign key of record.postid is post.id.

Now we need to find the articles in the post table based on the postid in the record table. (Assume that the reading order is: 'Article 1, Article 2, Article 2, Article 3, Article 3' allows repeated articles.)
I started with

<code>SELECT *
FROM post
WHERE id IN (postid1,postid2,postid2,postid3,postid3);</code>

The search results can only find articles with postid1, postid2, and postid3.

The business logic we want now is:
1: Find articles in the post table based on the user’s reading record.
2: Duplication is allowed.

For example, if the reading order of the articles read by the user is: 'Article 1, Article 2, Article 2, Article 3, Article 3', the result of searching the post table is: post.id1, ​​post.id2, post.id2, post.id2 ,post.id3.

Note: Consider using FOR loop

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

But I am worried 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

It can provide an idea: use in to retrieve data from the database, and use map to establish the corresponding relationship map.put(postid, post). Then take it out in the order of ids and assemble the required data types

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn