sona das
sona das

Reputation: 1439

Update row with values of other column's row of same Table in mysql

I have a table:-

 +----------+--------------+-------------------+-----------+
 | IsActive | IsFirstChunk | TotalResponseTime | StallTime |
 +----------+--------------+-------------------+-----------+
 | Yes      | Yes          | 62                | NULL      |
 | NULL     | NULL         | 327               | 0         |
 | NULL     | NULL         | 59                | 0         |

I want to fill the row of the column StallTime with value of TotalResponseTime only for the row whose column IsActive and IsFirstChunk is marked Yes, so my table will look like this:-

 +----------+--------------+-------------------+-----------+
 | IsActive | IsFirstChunk | TotalResponseTime | StallTime |
 +----------+--------------+-------------------+-----------+
 | Yes      | Yes          | 62                | 62        |
 | NULL     | NULL         | 327               | 0         |
 | NULL     | NULL         | 59                | 0         |

Upvotes: 4

Views: 38

Answers (2)

denny
denny

Reputation: 2254

you can do this by applying condition in where clauses. if IsActive and IsFirstChunk match 'yes'

update a1  
set stallTime = TotalResponseTime 
where IsActive in ('yes') and IsFirstChunk in ('yes');

another way

update a1  
set stallTime = TotalResponseTime 
where IsActive is not null and IsFirstChunk is not null;

Upvotes: 1

jophab
jophab

Reputation: 5509

You can use simple update statement which assigns the value of TotalResponseTime to StallTime where IsActive and IsFirstChunk is yes

 update tbl set StallTime=TotalResponseTime where 
    IsActive='Yes' and IsFirstChunk='yes';

Upvotes: 1

Related Questions