Why can't I use SELECT … FOR UPDATE with aggregate functions?

后端 未结 3 942
臣服心动
臣服心动 2021-01-01 22:23

I have an application where I find a sum() of a database column for a set of records and later use that sum in a separate query, similar to the following (made up tables, bu

3条回答
  •  春和景丽
    2021-01-01 23:14

    Is your problem "However, in theory someone could update the cost column on the materials table between the two queries, in which case the calculated percents will be off."?

    In that case , probably you can simply use a inner query as:

    SELECT material_id, cost/(SELECT Sum(cost)
      FROM materials
      WHERE material_id >=0
      AND material_id <= 10)
    INTO v_material_id_collection, v_pct_collection
    FROM materials
    WHERE material_id >=0
    AND material_id <= 10;
    

    Why do you want to lock a table? Other applications might fail if they try to update that table during that time right?

提交回复
热议问题