mysql - SQL: UPDATE from a complex select -


in phone system scenario have 2 tables.

  • table1 composed by: customer_id, call_duration, calldate, skip_billing .
  • table2 composed by: customer_id, bonus_seconds.

table1 stores calls customers , table2 stores bonus_seconds represents free conversation time allowed defined customer(ie: customer 1 first 40 cumulative seconds free).

i have write query update table1 according condition explained below: set skip_billing within calls defined free in table2.

so first need group customer_id , iterate on calls, incrementing cumulative variable(cumsec) on call_duration , set skip_billing accordingly.

table1 example is:

|sqlid |customer_id |billsec | skipbill| |0     |1           |12      | 1       |<--need set 1 due cume=12 customer_id=1 |1     |1           |10      | 1       |<--need set 1 due cume=22 customer_id=1 |2     |1           |15      | 1       |<--need set 1 due cume=37 customer_id=1 |3     |1           |8       | 0       |<--nop(no operation) due cume=45 |4     |2           |12      | 1       |<--need set 1 due cume=12 customer_id=2 |5     |3           |12      | 1       |<--need set 1 due cume=12 customer_id=3 |6     |2           |12      | 0       |<--nop due cume=24 customer_id=2 |7     |1           |12      | 0       |<--nop due cume=49 customer_id=1 |8     |3           |15      | 0       |<--nop due cumsec=27 customer_id=3  |customer_id |bonus_seconds| |1           |40           | |2           |20           | |3           |15           | 

i tried query (thanks gordon linoff) returns right set of row:

    select t.cume, t.calldate, t.customer_id (select t.*, (@cume := @cume + billsec) cume table1 t cross join (select @cume := 0) vars order calldate) t, table2 sct t.cume <= sct.bonus_seconds , t.customer_id=sct.customer_id ; 

but when try use withing update below command not work because not match anything.

    update table1 set skipbill=1 sqlid=(select t.sqlid (select t.*, (@cume := @cume + billsec) cume table1 t cross join (select @cume := 0) vars order calldate) t, table2 sct t.cume <= sct.bonus_seconds , t.customer_id=sct.customer_id ) ; 

how can write update task using query or better ?

thank in advance

update table1  set skipbill = 1  sqlid in (     select distinct t.sqlid      (         select t.*, (@cume := @cume + billsec) cume          table1 t          cross join (select @cume := 0) vars          order calldate     ) t, table2 sct      t.cume <= sct.bonus_seconds            , t.customer_id = sct.customer_id ); 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -