我有3个不同的表balance
,received
,expenses
,其中包含以下数据。
收到的表格
mysql> select * from received;
+-----+---------+-----------------+---------------------+
| rid | site_id | received_amount | receive_date |
+-----+---------+-----------------+---------------------+
| 1 | 1 | 500 | 2015-08-19 18:16:51 |
| 2 | 1 | 600 | 2015-08-19 18:16:52 |
| 3 | 1 | 500 | 2015-08-20 18:16:52 |
| 4 | 1 | 500 | 2015-08-19 18:16:52 |
+-----+---------+-----------------+---------------------+
表费用
mysql> select * from expenses;
+-----+---------+----------------+---------------------+
| eid | site_id | expense_amount | expense_date |
+-----+---------+----------------+---------------------+
| 1 | 1 | 500 | 2015-08-19 18:17:11 |
+-----+---------+----------------+---------------------+
表余额:
mysql> select * from balance;
+----------------+---------+---------------+--------------+-----------------+-----------------+------+------+---------------------+
| transaction_id | site_id | account_title | particulars | opening_balance | closing_balance | rid | eid | transaction_date |
+----------------+---------+---------------+--------------+-----------------+-----------------+------+------+---------------------+
| 1 | 1 | test1 | test1 values | 0 | 500 | 1 | NULL | 2015-08-19 18:16:51 |
| 2 | 1 | test1 | test1 values | 500 | 1100 | 2 | NULL | 2015-08-19 18:16:52 |
| 3 | 1 | test1 | test1 values | 1100 | 1600 | 3 | NULL | 2015-08-20 18:16:52 |
| 4 | 1 | test1 | test1 values | 1100 | 1600 | 4 | NULL | 2015-08-19 18:16:52 |
| 5 | 1 | test1 | test1 values | 1600 | 1100 | NULL | 1 | 2015-08-19 18:17:11 |
+----------------+---------+---------------+--------------+-----------------+-----------------+------+------+---------------------+
我正在尝试使用以下查询将收到的金额和费用合并到余额表中,但不知怎的,我无法得到正确的方法来获取它。
select
b.transaction_id,
b.site_id,
b.account_title,
b.particulars,
b.opening_balance,
r.received_amount,
e.expense_amount,
b.closing_balance,
b.transaction_date
from
balance b
inner join received r
on b.site_id = r.site_id
inner join expenses e
on b.site_id = e.site_id
group by
b.transaction_id;
我正在尝试获得此输出
+----------------+---------+---------------+--------------+-----------------+-----------------+----------------+-----------------+---------------------+
| transaction_id | site_id | account_title | particulars | opening_balance | received_amount | expense_amount | closing_balance | transaction_date |
+----------------+---------+---------------+--------------+-----------------+-----------------+----------------+-----------------+---------------------+
| 1 | 1 | test1 | test1 values | 0 | 500 | NULL | 500 | 2015-08-19 18:16:51 |
| 2 | 1 | test1 | test1 values | 500 | 600 | NULL | 1100 | 2015-08-19 18:16:52 |
| 3 | 1 | test1 | test1 values | 1100 | 500 | NULL | 1600 | 2015-08-20 18:16:52 |
| 4 | 1 | test1 | test1 values | 1600 | NULL | 500 | 1100 | 2015-08-19 18:16:52 |
| 5 | 1 | test1 | test1 values | 1100 | 500 | NULL | 1600 | 2015-08-19 18:17:11 |
+----------------+---------+---------------+--------------+-----------------+-----------------+----------------+-----------------+---------------------+
答案 0 :(得分:0)
你有点亲近。而不是INNER JOIN,您需要对收据和费用进行LEFT-JOIN。因为你的平衡" table具有所有事务,它将驱动它在底层支持表中看到的条目。此外,不要只是加入网站ID,而只是加入" rID"和" eID"因为无论如何它们都是你桌上的主要钥匙。
无需按交易ID分组,因为它是余额表中的主键,并且只有给定ID的一个条目。
select
b.transaction_id,
b.site_id,
b.account_title,
b.particulars,
b.opening_balance,
r.received_amount,
e.expense_amount,
b.closing_balance,
b.transaction_date
from
balance b
LEFT join received r
on b.rid = r.rid
AND b.site_id = r.site_id
LEFT join expenses e
on b.eid = e.eid
AND b.site_id = e.site_id
order by
b.site_id,
b.transaction_date