tags:

views:

114

answers:

4

If only one row with a distinct field is to be updated,I can use:

insert into tab(..) value(..) on duplicate key update ...

But now it's not the case,I need to update 4 rows inside the same table,which have its field "accountId" equal to $_SESSION['accountId'].

What I can get out of my mind at the moment is:

delete from tab where accountId = $_SESSION['accountId'],

then insert the new rows.

Which obviously is not the best solution.

Has someone a better idea about this?

A: 

Use the update just like that!

update tab set col1 = 'value' where accountId = $_SESSION['accountId']

Moreover, MySQL allows you to do an update with a join, if that makes your life a bit easier:

update
    tab t
    inner join accounts a on
        t.accountid = a.accountid
set
    t.col1 = 'value'
where
    a.accountname = 'Tom'
Eric
This sounds too simple, I am curious what the update will be.
James Black
@James: `update` is ANSI SQL and is not too simple at all. The first query will do *exactly* what the OP asked. I highly suggest you grab a copy of MySQL or SQLite and try it out on your machine!
Eric
No,maybe I'm to make it clearer.I'm going to update those rows with different value,not what you did now.
Shore
@Shore: Please post a simplified desired before and after so I can tailor it for you.
Eric
Say,if there are 4 rows.For the first row,I'm going to update its value to col1 = 'value1',for second,col1 = 'value2',so on...
Shore
What's in those rows now? Does it matter which one gets `value1` and which one gets `value2`? What columns are you using to determine that?
Eric
The order of rows doesn't matter.You can simplify it by supposing there are only accountId and col1 in the table.
Shore
@Shore: Then you aren't doing an update. You're inserting 4 new rows and just trying to do it via an `update` for some reason.
Eric
But there can only be 4 rows.So each time I do a delete job.The best solution should be update the original 4 rows.
Shore
Only update your rows if they're unique or they matter which row gets what value. Otherwise, you'll never be sure what row you're assigning the value, and may leave some values there that shouldn't be there. If you need to nuke the table every time, you can always do `truncate table tab` which will erase all entries of a table immediately. Can I ask why there's only ever 4 rows in the table, and what you're using this for?
Eric
Sorry,I mean 4 rows with accountId=$accountId
Shore
@Shore: The point stands though that you're replacing them, but you aren't updating them.
Eric
A: 

Based on your question, it seems like you should review the Update Statement.

Insert is used to put new rows in - not update them. Delete is used to remove. And Update is used to modify existing rows. Using "Insert On Duplicate Key Update" is a hackish way to modify rows, and is poor form to use when you know the row is already there.

Tom Ritter
A: 
  1. load all of the values in to a temporary table.
  2. UPDATE all of the values using a JOIN.
  3. INSERT all of the values from the temp table that don't exist in the target table.
longneck
A: 

You can use replace statement. This will work as a DELETE followed by INSERT

jab