Skip to content
Advertisement

How to update attribute based on result of aggregate function

I am new to mySQL. I want to update the order price of a table, based on the value of the retail price and the quantity of the product ordered. I have the following tables (simplified for this question):

Products:

Sales_orders:

Sales_products:

The following code works in that I get the correct Order_price and it outputs in its own table. But I want to update the attribute Sales_orders.Order_price, not call out the order price in a table.

I’ve also tried a trigger, but this returns an error when I try to insert a new row in Sales_orders.

Error returned :

Error Code: 1222. The used SELECT statements have a different number of columns

Hope somebody can help with this?

Advertisement

Answer

This is a trigger, so I expect it to be using new or old.

This would usually be done incrementally:

You can recalculate the entire value if you like:

However, triggers are usually done incrementally.

Remember that if you do this, you will also need update and delete triggers as well. That is why it is simpler to calculate these values “on-the-fly” rather than maintaining them using triggers.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement