我有兩個表。表「用戶」包含user_id,user_name。表「交易」包含USER_ID,交易如何在一個表中顯示多行,並在一些計算後在另一個表中顯示一行
如: 表USER:
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 1 | Ram |
| 2 | John |
+---------+-----------+
內幕交易
+---------+---------+--------------+
| 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 |
+---------+---------+--------------+
我想將所有的信貸量來顯示結果並扣除所有借方發生額如下圖所示:
+-----------+--------------+-------------+-------------+
| user_name | Total Credit | Total debit | Grand Total |
+-----------+--------------+-------------+-------------+
| Ram | 950 | 550 | 400 |
| John | 350 | 50 | 300 |
+-----------+--------------+-------------+-------------+
我該怎麼做?
忘記整齊地格式化我的問題的方法不止一種。 – user1010966