sql - How to use update and select form the same table ? mysql -


i have table :

`id`      `activity_time`  1    0000-00-00 00:00:00  2    2015-06-1 12:12:12  3    0000-00-00 00:00:00  4    0000-00-00 00:00:00  5    2015-06-2 13:13:13 

i want update every row date "0000-00-00 00:00:00" , set first next non "0000-00-00 00:00:00" row.

so id #1 set 2015-06-1 12:12:12.

and #3 , #4 set 2015-06-2 13:13:13.

i trying run query :

update        table1       set activity_time =           (              select activity_time table1 t2                           t2.activity_time != '0000-00-00 00:00:00'               ,              t2.id > table1.id              order id asc              limit 1           )       activity_time = '0000-00-00 00:00:00' 

but go error :

you can't specify target table 'table1' update in clause. 

in mysql can use join this:

update table1 t1 join        (select t1.*,                (select t1b.activity_time                 table1 t1b                 t1b.activity_time <> '0000-00-00 00:00:00' ,                       t1b.id > t1.id                 order t1b.id asc                 limit 1                ) next_activity_time         table1 t1a         t1a.activity_time <> '0000-00-00 00:00:00'       ) tnxt       on tnxt.id = t1.id       set t1.activity_time = tnxt.next_activity_time       t1.activity_time = '0000-00-00 00:00:00'; 

the basic idea rewrite query select values want. join query information update.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -