reterieving the most recently added data from MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Norgy
    New Member
    • May 2013
    • 56

    reterieving the most recently added data from MySQL

    i want to get the most recently added data from MySQL
    for example, if we have a table named test with column : id
    the data is inserted as follows: 2, 4, 1, 3
    when i type this query :
    Code:
    SELECT * FROM test ORDER BY id DESC LIMIT 1
    , it returns 4
    but i want a query that returns 3 (the last inserted data)
    can i do this?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use an auto increment field or include a column with a timestamp.

    Comment

    • Norgy
      New Member
      • May 2013
      • 56

      #3
      i tried the auto increment field, but it makes me drop the primary key of the table and make that field the pk
      i want the table as it is without changing anything in it

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        The auto increment should probably be the primary key. Any other keys should be a unique index. But if you don't want to do it that way, then use the timestamp field instead.

        If you mean you want to do that query without changing the table at all, then it's impossible. You need a way to identify when the row was added so you can find the most recent. And to do that, you need to add a field because you currently have nothing to identify the most recent row.

        Comment

        • Norgy
          New Member
          • May 2013
          • 56

          #5
          i used the timestamp field and it works correctly
          thanks a lot :)

          Comment

          Working...