Sql_ Query the current status of each tid: that is, the latest publication of the category

Source: Internet
Author: User

First, the problem scenario

  1. A table has a key field tid,action,dateline, etc., the TID represents the id,action of the Post, which represents the status of the post being manipulated, Dateline represents the timestamp of the data being inserted;

  2. In this table, each TID will have several different Dateline inserted details, the last time the action field in the detail inserted indicates the status of the current Tid's post;

  3. Now you want to query the current status of each tid, that is, to query the latest published record

  4. Sample Data in table:


Second, SQL statement:

Select A.* from Pre_forum_threadmod as A, (select Tid,max (Dateline) as Dateline from Pre_forum_threadmod Group by Tid) as B where A.tid=b.tid and A.dateline=b.dateline

Third, SQL statement parsing:

  1. In the above SQL statements, the largest dateline of each TID is identified, and the query generates a temporary table B;

    (select Tid,max (Dateline) as Dateline from Pre_forum_threadmod Group by Tid) as B
  2. Then the original table A and temporary Table B Association query can:

    where A.tid=b.tid and A.dateline=b.dateline
  3. After enquiry, for example, the latest record of each TID can be found:

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.