+----+------------+----------------+--------+
| id | dateValue | particular | amount |
+----+------------+----------------+--------+
| 1 | 2017-02-02 | Cash Deposit | 1 |
| 2 | 2017-02-03 | Cheque Deposit | 2 |
| 3 | 2017-02-08 | Cash Deposit | 3 |
| 4 | 2017-02-07 | Product Sales | 4 |
+----+------------+----------------+--------+
+----+------------+------------+--------+
| id | dateValue | particular | amount |
+----+------------+------------+--------+
| 1 | 2017-05-07 | Factory | 6 |
| 2 | 2017-06-02 | Staff | 7 |
| 3 | 2017-06-03 | Travel | 8 |
+----+------------+------------+--------+
I then used select * from income left join expenses on income.id = expenses.id
to query the database and got:
+----+------------+----------------+--------+----+------------+------------+--------+--+
| id | dateValue | particular | amount | id | dateValue | particular | amount | |
+----+------------+----------------+--------+----+------------+------------+--------+--+
| 1 | 2017-02-02 | Cash Deposit | 1 | 1 | 2017-05-07 | Factory | 6 | |
| 2 | 2017-02-03 | Cheque Deposit | 2 | 2 | 2017-06-02 | Staff | 7 | |
| 3 | 2017-02-08 | Cash Deposit | 3 | 3 | 2017-06-03 | Travel | 8 | |
| 4 | 2017-02-07 | Product Sales | 4 | | | | | |
+----+------------+----------------+--------+----+------------+------------+--------+--+
Which based on the currently available information is probably the best I can do
You could the use something like Most simple code to populate JTable from ResultSet or Retrieving Data from JDBC Database into Jtable to build the JTable
... as available examples
if second table have more row than table one .. record will not display which are more id in second table – raman dangol 8 hours ago
Also, if there are inconsistent IDs, the records will be lost. That's why I said this was not a trivial question
Then in those cases, something like FULL OUTER JOIN would be more useful. However, based on the information I have, full outer join
is not supported in MySQL, because that would be useful. However, based on How to do a FULL OUTER JOIN in MySQL? we could do something like...
select * from income left join expenses on income.id = expenses.id union select * from income right join expenses on income.id = expenses.id where income.id is null
which can generator something like
+----+------------+----------------+--------+----+------------+------------+--------+
| id | datevalue | particular | amount | id | datevalue | particular | amount |
+----+------------+----------------+--------+----+------------+------------+--------+
| 1 | 2017-02-02 | Cash Deposit | 1.0 | 1 | 2017-05-07 | Factory | 6.0 |
| 2 | 2017-02-03 | Cheque Deposit | 2.0 | 2 | 2017-06-02 | Staff | 7.0 |
| 3 | 2017-02-03 | Cash Deposit | 3.0 | 3 | 2017-06-03 | Travel | 8.0 |
| 4 | 2017-02-03 | Product Sales | 4.0 | 4 | 2017-10-01 | Test 1 | 10.0 |
| 5 | 2017-10-02 | Test 2 | 20.0 | | | | |
+----+------------+----------------+--------+----+------------+------------+--------+
Or, if you prefer to keep things aligned to their "selected sides", something like
select income.id, income.datevalue, income.PARTICULAR, income.AMOUNT,
expenses.id, expenses.datevalue, expenses.PARTICULAR, expenses.AMOUNT
from income join expenses on income.id = expenses.id
union all
select income.id, income.datevalue, income.PARTICULAR, income.AMOUNT,
null, null, null, null
from INCOME where not exists (select expenses.id from expenses where expenses.id = income.id)
union all
select null, null, null, null,
expenses.id, expenses.datevalue, expenses.PARTICULAR, expenses.AMOUNT
from expenses where not exists (select income.id from income where income.id = expenses.id)
Which can generate something like...
+----+------------+----------------+--------+----+------------+------------+--------+
| id | datevalue | particular | amount | id | datevalue | particular | amount |
+----+------------+----------------+--------+----+------------+------------+--------+
| 1 | 2017-02-02 | Cash Deposit | 1.0 | 1 | 2017-05-07 | Factory | 6.0 |
| 2 | 2017-02-03 | Cheque Deposit | 2.0 | 2 | 2017-06-02 | Staff | 7.0 |
| 3 | 2017-02-03 | Cash Deposit | 3.0 | 3 | 2017-06-03 | Travel | 8.0 |
| 4 | 2017-02-03 | Product Sales | 4.0 | 4 | 2017-10-01 | Test 1 | 10.0 |
| | | | | 5 | 2017-10-02 | Test 2 | 20.0 |
+----+------------+----------------+--------+----+------------+------------+--------+
At the end of the day, it's still a database issue.
For simplicity, you could create one or more database views
in order to simply the query