2

this is an example:

table mysql schema :

 id name salary 1 david 20 2 jack 30 3 david 10 

Query:

  $sql = "UPDATE table SET salary = salary + 5 WHERE name = 'david' ";  

I want to added Group By name to avoid duplicate update for david How Could I do that?

3
  • 2
    What row will you update, 1 or 3 in your example? Commented Aug 22, 2011 at 18:23
  • Do you want to update both rows or just one of them? If so, you'd need to use a different query by using the id and not name. Commented Aug 22, 2011 at 18:23
  • This doesn't even make sense. How would GROUP BY define which david you're referring to? Commented Aug 22, 2011 at 18:23

1 Answer 1

2

Try using LIMIT, which is an extension to SQL used by MySQL:

$sql = "UPDATE table SET salary = salary + 5 WHERE name = 'david' ORDER BY id LIMIT 1"; 

It makes no sense to use GROUP BY, because it would be ambiguous whether to update the first row in the group, or the last row, or all of the rows. MySQL does not support a GROUP BY clause in the UPDATE statement.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.