Answer the question
In order to leave comments, you need to log in
How to correctly query with SUM to MY SQL?
Good afternoon.
Mysql database has 2 tables:
1st with users:
+---------------+--------------+------+ -----+---------+----------------+
| field | type | Null | key | default | extra |
+---------------+------+------+-----+----- ----+----------------+
| USER_ID | int(11) | NO | PRI | NULL | auto_increment |
| GROUP ID | int(11) | YES | MUL | NULL | |
| USER_NAME | varchar(255) | YES | | NULL | |
| BALANCE | double | YES | | 0 | |
| BALANCE_TIME | int(11) | YES | | NULL | |
| CARDID | int(11) | YES | | NULL | |
| PREPAID_DAY | bigint(20) | YES | | NULL | |
| PREPAID_WEEK | bigint(20) | YES | | NULL | |
| PREPAID_MONTH | bigint(20) | YES | | NULL | |
| DELETED_TIME | bigint(20) | YES | | 0 | |
+---------------+------+------+-----+----- ----+----------------+
And sessions:
+---------------+-------- -----+------+-----+---------+----------------+
| field | type | Null | key | default | extra |
+---------------+-----+------+-----+------ ---+----------------+
| SESSION_ID | int(11) | NO | PRI | NULL | auto_increment |
| USER_ID | int(11) | YES | MUL | NULL | |
| GROUP ID | int(11) | YES | MUL | NULL | |
| TIME_BEGIN | int(11) | YES | MUL | NULL | |
| TIME_END | int(11) | YES | | NULL | |
| BYTES_RECV | bigint(20) | YES | | NULL | |
| BYTES_SENT | bigint(20) | YES | | NULL | |
| BYTES_PREPAID | bigint(20) | YES | | NULL | |
| cost | double | YES | | NULL | |
| COST_TIME | double | YES | | NULL | |
| VPN_SESSION | smallint(6) | YES | | NULL | |
+---------------+-----+------+-----+------ ---+----------------+
I want to calculate for each user the amount of traffic BYTES_RECV bBYTES_SENT , for this you need to sum it for each user.
My query counts only by the first USER_ID ..
SELECT users.USER_NAME, SUM(sessions.BYTES_RECV), SUM(sessions.BYTES_SENT
), SUM(.sessions.BYTES_RECV+sessions.BYTES_SENT) as COMMON FROM users LEFT JOIN
sessions ON users.USER_ID=sessions.USER_ID ;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question