mysql - Inner join on more than 2 tables -


i have 3 different tables balance,received,expenses following data in it. table received:

mysql> select * 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 * expenses; +-----+---------+----------------+---------------------+ | eid | site_id | expense_amount | expense_date        | +-----+---------+----------------+---------------------+ |   1 |       1 |            500 | 2015-08-19 18:17:11 | +-----+---------+----------------+---------------------+ 

table balance:

mysql> select * 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 trying merge amount of received , expenses balance table using following query somehow not able correct way 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            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        b.transaction_id; 

i trying 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 close. instead of inner join, need left-join receipts , expenses. since "balance" table has transactions, drive entry sees within underlying support tables. also, don't join on site id, "rid" , "eid" since primary keys on table anyhow.

no need group transaction id primary key in balance table , have 1 entry given 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            balance b           left join received r              on b.rid = r.rid             , b.site_id = r.site_id           left join expenses e              on b.eid = e.eid             , b.site_id = e.site_id     order       b.site_id,       b.transaction_date 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -