简体   繁体   English

内联超过2张桌子

[英]Inner join on more than 2 tables

I have 3 different tables balance , received , expenses with following data in it. 我有3个不同的表balancereceivedexpenses与在它下面的数据。 Table received: 表收到:

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 |
+-----+---------+-----------------+---------------------+

Table expenses: 餐桌费用:

mysql> select * from expenses;
+-----+---------+----------------+---------------------+
| eid | site_id | expense_amount | expense_date        |
+-----+---------+----------------+---------------------+
|   1 |       1 |            500 | 2015-08-19 18:17:11 |
+-----+---------+----------------+---------------------+

Table balance: 表余额:

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 |
+----------------+---------+---------------+--------------+-----------------+-----------------+------+------+---------------------+

I am trying to merge the amount of received and expenses into balance table using following query but somehow I am not able to get correct way to get it. 我正在尝试使用以下查询将收支金额合并到余额表中,但是以某种方式我无法获得正确的方法。

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;

I am trying to get this output 我正在尝试获得此输出

+----------------+---------+---------------+--------------+-----------------+-----------------+----------------+-----------------+---------------------+
| 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 |
+----------------+---------+---------------+--------------+-----------------+-----------------+----------------+-----------------+---------------------+

You are somewhat close. 你有点接近。 Instead of INNER JOIN, You need to do a LEFT-JOIN to the receipts and expenses. 您需要对收款和费用进行左联接,而不是内联接。 Since your "Balance" table has ALL transactions, it will drive which entry it sees within your underlying support tables. 由于您的“余额”表具有所有事务,因此它将驱动其在基础支持表中看到的条目。 Also, don't just join on the site ID, but just the "rID" and "eID" since they would be your primary keys on the table anyhow. 另外,不要仅加入站点ID,还应该加入“ rID”和“ eID”,因为无论如何它们都是您在表上的主键。

No need to group by transaction ID as that is the primary key in the balance table and would only have one entry by a given ID. 无需按交易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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM