Database information to re-query

Source: Internet
Author: User

For programming is to make a variety of data processing, and in general, the final storage of data is the database, the database of SQL operations is very important. If a table in a database holds a large amount of data, and two of the tables have the same data, how can you query the last or the first data of the records associated with this field based on the same field value?

First, simply as an experimental point of view, you first need to create a table, and then fill a small amount of data, one or some of the field values are the same, this time may need to copy, insert such SQL operation statement:

Create a table message, and then populate the underlying data, and then the conditional copy data is written to the same table:

INSERT into AAA (Account,content,time) SELECT account,content,time from AAA WHERE id=1;

Note: Remember to modify the value of the Time field, because this instance is based on the timing of the value, but also according to their own needs to set the value of the condition.

The second step is now to start taking values.

For example, I want to get the last communication record for each account: SELECT * from Message WHERE time in (select MAX (time) from Message GROUP by).

Note: You can also just take the first piece of data, just modify the max () function to query at once for Min ().

Database information to re-query

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.