Cheerio
Cheerio

Reputation: 1240

MySQL: update field with Group By

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?

Upvotes: 2

Views: 817

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 563021

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.

Upvotes: 2

Related Questions