PHP and MYSQL Help

stesouthby

New Member
Messages
115
Reaction score
0
Points
0
Hi I have a transaction database where when you up date the traction of a product it is place on the that as like 200 or -50 and it carrys on like this in a list I was wondering what code would i need to do to add all the mysql tranactions up to get a total and place it on in a products table or displays it on the home page any help would be greatful i know i will need to do a mysql query to get all the ones i need i just don't know where to go from there
 

stesouthby

New Member
Messages
115
Reaction score
0
Points
0
ok right if you have a list of numbers in a mysql database is the away to add and subtract them all to get a total for example if it would be listed as ethier 20 or -20 is there away to do that?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
SQL has aggregate functions that operate on grouped columns, returning a single value for the group. In this case, SUM should do what you want.

Code:
SELECT SUM(delta) FROM transactions GROUP BY product;

If you need something more specific, you'll need to include a minimal test case, which (for this question) would be a table schema (definition) and test data. For example,

Code:
CREATE TABLE transactions (
    id INT PRIMARY KEY AUTO_INCREMENT,
    delta INT,
    product INT /* REFERENCES products (id) ON DELETE CASCADE ON UPDATE CASCADE */,
) Engine=InnoDB;

INSERT INTO transactions (delta, product)
  VALUES
    (20, 0),
    ...
  ;

Also, this.
 
Last edited:
Top