开发者

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 开发者_JAVA技巧Group By name to avoid duplicate update for david How Could I do that?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜