Use native SQL queries to deliver the latest articles to the homepage by using the split analogy

Source: Internet
Author: User

Generally on the homepage of the site will have the latest articles of the site push, and these articles belong to different categories. If a category of articles suddenly focus on a time period of publishing, then it will cause all the articles on the home page is the classification of the article, the other article classification becomes invisible. So, I hope I can improve on it.

Ideas

Once the site has the latest articles published then the article and its classification will be pushed to the homepage of the site, if there are new articles published in this category, then Post will replace the published article. Each category can push no more than n articles.

Workaround

In order to solve this problem, I first thought about using group by, so that all articles are grouped by classification number and then sorted in descending order by publication time.

The SQL code can be represented as:

Select *  from Group  by Order  by desc

However, after doing this I found that, although the article according to the classification number, that is, each classification number only one article in the result set, but, because the first group and then by the publication time descending order, which led to the first by the classification number in ascending order, on this basis and then by time descending, This results in the latest published articles not necessarily appearing on the homepage. It seems that the problem is not as simple as thought.

Workaround

After a day of hard searching and several attempts, finally found a solution, the following SQL statement can be used to send the first page by sub-analogy to the latest published N article collection (10 articles), the code is as follows:

SelectA1. Article name, A1. Release time, A1. Classification number fromArticle table A1Inner Join (      SelectA. Classification number, a. Release time fromArticle Table A Left JoinArticle Table B onA. Classification number=B. Classification number andA. Release time<=B. Release timeGroup  byA. Classification number, a. Release time having Count(b. Release time)<=N) B1 onA1. Classification number=B1. Classification number andA1. Release Time=B1. Release TimeOrder  byA1. Release TimedescLimitTen

If you have time, you can deduce the whole process on paper yourself. Pro-Test, code available. If you encounter a similar problem, it will certainly help. If you have a better way, you can also give me a message.

This article starts in the top to seek the net, reprint please indicate the source.

Use native SQL queries to deliver the latest articles to the homepage by using the split analogy

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.