Answer the question
In order to leave comments, you need to log in
How to change rows in a relational database?
Good evening!
There are 3 tables, namely:
STORAGE with id and count fields,
combinations with storage_id, product_id,count fields
AND products with id
field Combinations.count must be subtracted from the storage.count field, for all rows with storage.id = storage_id and product_id = product.id
In this case, the selection goes by products.id with the following query:
UPDATE `storage`
LEFT JOIN `combinations` ON `combinations`.`storage_id` = `storage`.`id`
LEFT JOIN `products` ON `products`.`id` = `combinations`.`product_id`
SET `storage`.`count` = `storage`.`count` - `combinations`.`count`
WHERE `products`.`id` IN (?ai)
Answer the question
In order to leave comments, you need to log in
In general, a dangerous approach, because the script can "accidentally" work more than the required number of times and you will have all the values broken. I never do that. I load the values and do all the calculations in the script, so the values remain unchanged.
In general, it is not a very healthy practice to change data in this way. IMHO.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question