开发者

adding mysql table values

I ha开发者_StackOverflow社区ve a table in my database that holds numerical values collected from a user's input. How could I add those values together and display that number on the website, with the number updating every time a new number is inputed.


SELECT SUM(value) FROM table

Something like this? You should also look into GROUP BY.


EDIT:

It could be you're meaning that you have a value and you want to increment it by n. Then you can look at this example code.

UPDATE table SET value = value + n WHERE id = 123

Where n is the value you want to increment it by.


I would query a master table of IDs that has the running total of values.

Then, via any inserts into some alternate table that keeps each individual entry accounted for, there is a trigger that forces a SQL-Update to the master table... This way, you don't have to keep doing a web-based query that is always doing a GROUP BY for the results.

If this is a little confusing, think of an inventory system. You have one master item table of all possible inventory items. It has an "on hand" count. Then, as sales of an item are sold, the "on hand" count is reduced by however many are purchased. You are not going to each individual sales order and counting grouped by a given ID, you just go to thee master inventory item table and have that "on hand" count.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜