Solved

Postgresql Querying the last 10 records from the database

Posted on 2004-10-24
251 Views
Last Modified: 2008-03-06
Hi Sir,
Can you help me in writing a query for retrieving Postgresql Querying the last 10 records (recent) from the database. Its urgent
0
Question by:ekartha
    3 Comments
     
    LVL 32

    Accepted Solution

    by:
    If you just want to retrieve the last 10 records which were inserted (not considering update),      with your auto_increment column, you can use:
    SELECT * FROM yourtablename ORDER BY your_auto_increment_column LIMIT 10;
          
    PS: ORDER BY your_auto_increment_column is optional. This should be enough:
    SELECT * FROM yourtablename LIMIT 10;
          
    However, if the records are updatable and you want to retrieve the last 10 records which were accessed (either updated or inserted records), you need a date column like 'last_modified' which stores the date & time at which a record was inserted or updated.
          
    Then you can use:
    SELECT * FROM yourtablename ORDER BY last_modified_column LIMIT 10;
    0
     
    LVL 9

    Assisted Solution

    by:rjkimble
    I think you need to add "DESCENDING" to those ORDER BY clauses to achieve what you want.
    0
     
    LVL 32

    Expert Comment

    by:ldbkutty
    yes, good point. So, the query should be like:
    SELECT * FROM yourtablename ORDER BY corresponding_col DESC LIMIT 10;
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Ora2pg 4 71
    Insert bulk records in fact table 2 412
    Script To Save Results Of A Query in PostgreSQL 1 305
    phpPgAdmin problem 14 29
    Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
    Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now