Home  >  Article  >  Backend Development  >  After dividing the table, is there a good way to get the data sorted by time?

After dividing the table, is there a good way to get the data sorted by time?

WBOY
WBOYOriginal
2016-08-10 09:07:191565browse

There are three tables:

User table: user-------------------user_id


Article table: article----------------article_id,user_id;


Comment form: article_comment----comment_id,article_id


The user table has a one-to-many-to-many relationship with the article_comment table.

Now, article_comment is divided into tables. According to article_id%6, it is divided into six tables article_comment_0/1/2/3/4/5.
Now we need to display the comments of all articles of a certain user according to the time of the comment. How to use SQL What about writing?

Reply content:

There are three tables:

User table: user-------------------user_id


Article table: article----------------article_id,user_id;


Comment form: article_comment----comment_id,article_id


The user table has a one-to-many-to-many relationship with the article_comment table.

Now the article_comment is divided into tables. According to article_id%6, it is divided into six tables article_comment_0/1/2/3/4/5.
Now we need to display the comments of all articles of a certain user according to the time of the comment. How to use SQL What about writing?

<code>SELECT * FROM((SELECT * FROM article_comment_0 INNER JOIN article ON article_comment_0.article_id=article.article_id WHERE article.user_id='userid') UNION
(SELECT * FROM article_comment_1 INNER JOIN article ON article_comment_1.article_id=article.article_id WHERE article.user_id='userid') UNION
(SELECT * FROM article_comment_2 INNER JOIN article ON article_comment_2.article_id=article.article_id WHERE article.user_id='userid') UNION
(SELECT * FROM article_comment_3 INNER JOIN article ON article_comment_3.article_id=article.article_id WHERE article.user_id='userid') UNION
(SELECT * FROM article_comment_4 INNER JOIN article ON article_comment_4.article_id=article.article_id WHERE article.user_id='userid') UNION
(SELECT * FROM article_comment_5 INNER JOIN article ON article_comment_5.article_id=article.article_id WHERE article.user_id='userid')) res
ORDER BY res.comment_createtime DESC
</code>

If your table is designed like this, you need to connect the article query every time, so it is recommended to add a redundant user_id field in the common table, so that the query does not require one more step

If you can do it without splitting the tables, don’t split the tables. Split tables will bring a lot of trouble to development. You can use partitions. I don’t know much about mysql, but I found this:

http://dev.mysql.com/doc/refm...

The table should be divided according to your query conditions, otherwise there is no good way

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