I have two tables. Table “user” contains user_id, user_name. Table “Transactions” contains user_id, transactions
Eg:
TABLE USER:
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 1 | Ram |
| 2 | John |
+---------+-----------+
TABLE Transactions
+---------+---------+--------------+
| user_id | type | transactions |
+---------+---------+--------------+
| 1 | credit | 500 |
| 1 | debit | 300 |
| 2 | credit | 250 |
| 1 | credit | 450 |
| 2 | credit | 100 |
| 1 | debit | 250 |
| 2 | debit | 50 |
+---------+---------+--------------+
I want to display the result by adding all credit amount and deduct all debit amount as shown below:
+-----------+--------------+-------------+-------------+
| user_name | Total Credit | Total debit | Grand Total |
+-----------+--------------+-------------+-------------+
| Ram | 950 | 550 | 400 |
| John | 350 | 50 | 300 |
+-----------+--------------+-------------+-------------+
How can I do this?
Here’s the query: