MYSQL Update from within same table -


i have table data has gone wrong, need fix up. below example:

table-a id, type, value 1, 10, 123456 2, 10, null 3, 10, null 4, 20, 123456 5, 20, 654321 6, 20, null 

i need mysql update command.

if "type" same update "value" same long value null , "value" unique

update table-a set value = (...)

so in table above id 2 , 3 have value updated 123456

id 6 not update "value" not unique the same "type".

update table_a t          join            ( select type                   , min(value) value              table_a              group type              having count(distinct value) = 1            ) tu          on tu.type = t.type set t.value = tu.value t.value null 

as peufeu pointed, distinct needed catch cases one, suppose id=3 row has updated, too:

table-a id | type | value  1 |  10  | 123456  2 |  10  | 123456  3 |  10  | null  4 |  20  | 123456  5 |  20  | 654321  6 |  20  | null 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -