Bit of a newbie here. I’m currently working on a MySQL table that lists the details for different cars. I need a new field that is built up of the information from three other fields. So I have ‘Acceleration’, ‘Speed’ and ‘Braking’ which all contain double digit integers that are averaged out to another field I want to call ‘Average’.
The logic being ‘Acceleration’+’Speed’+’Braking’/3
I can’t seem to figure out the correct syntax to do this. I do specifically need this to be a field as I need those values to show up on other queries. I know a SELECT query can get the result values I need, but how to I conduct those values to a permanent field on that table?
Thanks in advance for any help on this.
First, you’d need to alter the table schema to define the new column:
Next, update the table to set the values:
Consider how to correctly set
Averagefor newly inserted/updated data. Perhaps use triggers:You might want to consider instead introducing this column in a view, to create the averages as required, on-the-fly, and thereby preventing it from becoming desynchronised from the underlying data values (but note you no longer achieve the performance benefit of having the values cached):
Finally, note that your average has no physical meaning in the real world (what would be its units?): a more meaningful metric may or may not be more suitable to your needs.