Avatar of lost_and_unfound
lost_and_unfound

asked on 

MySQL field calculations

Greetings,

I need to calculate the bandwidth usage for a user from RADIUS packets. I am using triggers to 'filter down' to the relevant sections to calculate.

in my traffic_usage table I have the following
mysql> describe traffic_usage;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | bigint(20)  | NO   | PRI | NULL    | auto_increment |
| month      | datetime    | NO   |     | NULL    |                |
| type       | varchar(32) | NO   |     | NULL    |                |
| username   | varchar(64) | YES  |     | NULL    |                |
| allowed    | bigint(20)  | YES  |     | NULL    |                |
| used       | bigint(20)  | YES  |     | NULL    |                |
| remaining  | bigint(20)  | YES  |     | NULL    |                |
| status     | varchar(20) | YES  |     | NULL    |                |
| rollover   | tinyint(1)  | YES  |     | 0       |                |
| created_at | datetime    | NO   |     | NULL    |                |
| updated_at | datetime    | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

Open in new window


Now using a trigger I pass the usage to the traffic_usage table after update with a trigger,
here is the part I am currently struggling with, calulating the remaining value
...
month       =   date_format(now(), '%Y-%m-00 00:00:00'),
type        =   'Mobile',
username    =   NEW.username,
allowed     =   (OLD.allowed - (NEW.acctinputoctets + NEW.acctoutputoctets)),
used        =   (OLD.allowed + (NEW.acctinputoctets + NEW.acctoutputoctets)),
remaining   =   ????
...

Open in new window


I am fairly new to this in-depth usage of MySQL and not sure what terms / keywords to google for...

Help gladly appreciated!
Thnks
MySQL ServerDatabases

Avatar of undefined
Last Comment
lost_and_unfound

8/22/2022 - Mon