Update last entry.

Discussion in 'MySQL' started by Jamie T, May 9, 2011.

  1. #1
    Hello. I have a form which will do some simple maths from the users inputs and insert it into the database with their record.

    Only way I can work it out is to put some code on the page they're taken to after submitting. Like this:

    UPDATE table SET table.total = (table.input1 * table.input2)

    This works but updates the whole table of course.

    I've been trying this to take the last record entered via a timestamp but it isn't working.

    UPDATE table SET table.total = (table.input1 * table.input2)
    ORDER BY table.updatetimestamp DESC LIMIT 0,1

    Can anyone tell me how it should be done please?

    Thanks.

    Jamie.
     
    Jamie T, May 9, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You are going to need to use a WHERE clause in your UPDATE query and either a sub-query or second query to identify which row to update. I'll generally explain how to do it with a second query:

    First run a query to identify the maximum value of the updatetimestamp column. Once you have that specific value, run your UPDATE query using that value as part of the WHERE clause.
     
    plog, May 9, 2011 IP
  3. rdthewall

    rdthewall Active Member

    Messages:
    1,225
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I second him. Adding a WHERE clause will solve your problem.
     
    rdthewall, May 12, 2011 IP