Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to get last 3 values from Mysql table

How to get last 3 values from Mysql table

Scheduled Pinned Locked Moved Database
mysqltutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 10263519
    wrote on last edited by
    #1

    hi, am adding new record for every transaction,then how to get the last 3 transaction values.

    S L 2 Replies Last reply
    0
    • M Member 10263519

      hi, am adding new record for every transaction,then how to get the last 3 transaction values.

      S Offline
      S Offline
      Snehasish_Nandy
      wrote on last edited by
      #2

      It would be best to have a TIMESTAMP column that defaults to CURRENT_TIMESTAMP .. it is the only true predictive behavior you can find here.

      The second-best thing you can do is ORDER BY ID DESC LIMIT 1 and hope the newest ID is the largest value.

      check this also accessing-last-inserted-row-in-mysql[^] Hope it will help..

      M 1 Reply Last reply
      0
      • S Snehasish_Nandy

        It would be best to have a TIMESTAMP column that defaults to CURRENT_TIMESTAMP .. it is the only true predictive behavior you can find here.

        The second-best thing you can do is ORDER BY ID DESC LIMIT 1 and hope the newest ID is the largest value.

        check this also accessing-last-inserted-row-in-mysql[^] Hope it will help..

        M Offline
        M Offline
        Member 10263519
        wrote on last edited by
        #3

        Not only one record. there exist duplicates(Means no primary key) get the last inserted 3 records for the same id

        1 Reply Last reply
        0
        • M Member 10263519

          hi, am adding new record for every transaction,then how to get the last 3 transaction values.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 10263519 wrote:

          am adding new record for every transaction,then how to get the last 3 transaction values

          You can't get them at transaction-level, as they don't exist at that level. A transaction can touch multiple tables, so those 3 "last values" might be all in different tables (or in the same record). If you're trying to write an audit, look into triggers.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups