문제

ID     GRP     VAL    CHK
---   -----   -----   ----
1       1       1      0
2       1       3      0
3       2       7      0
4       2       2      0
5       2       1      0
6       3       5      0

I want to set my CHK field to '1' having maximum value VAL for every group of GRP, so ID 2,3,6 should be set. I don't write my trials here, all seems rubbish :)

도움이 되었습니까?

해결책

In MySQL, you can do this using the update/join syntax:

update table t join
       (select grp, max(val) as maxval
        from table t
        group by grp
       ) tmax
       on t.grp = tmax.grp and t.val = tmax.maxval
    set t.chk = 1;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top