search

Home  >  Q&A  >  body text

How to batch update mysql according to respective conditions

There are two tables, A is the detailed table
id uid cost

    1. 0

    1. 1

    1. 4

    1. 3

Now we need to group the data with cost greater than 0 in the table according to uid and update it to another table

select uid,count(cost) as count from A group by uid where cost > 0

This is the statement taken out. Is there any way to update the query results to another table based on uid in one sentence?

代言代言2708 days ago974

reply all(1)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-26 10:51:25

    update B b set b.col=(select count(a.cost) as count from A a where cost > 0 and a.uid=b.uid group by uid)

    reply
    0
  • Cancelreply