I am very new to MySQL and also this is my first question if there is any mistakes please forgive me.
I have 6 tables i want to take report using this 6 tables. One of my report to access all the 6 tables and print the data as per the Query we write.Now the problem is i have write my query but this will return lot of duplicate data.How to avoid that guide me please.
Demo Data:
add_employees
-------------------
| eid | name |
-------------------
| 1 | Mohanraj |
-------------------
| 2 | pradeep |
-------------------
| 3 | kumar |
-------------------
| 4 | Murali |
-------------------
add_vehicle
---------------------
| vid | vnumber |
---------------------
| 1 | TN22BQ6226 |
---------------------
| 2 | TN37CM9014 |
---------------------
| 3 | TN38BR9217 |
---------------------
| 4 | TN38BT5680 |
---------------------
third_table
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| tid | vid | emp_id | entry_date | pick_place | start_time | drop_place | stop_time | pickupkm | drops | type_of_trip | travelkm | tamt | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 5-5-2017 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000 |2017-05-05 |
| 2 | TN22BQ6226 | 3 | 6-5-2017 | Hopes | 09:44 am | ESI | 12:30 pm | 12500 | 2500 | Cash | 1250 | 3500 |2017-05-06 |
| 3 | TN22BQ6226 | 3 | 5-5-2017 | Place1 | 03:15 pm | Place2 | 06:30 pm | 1500 | 1800 | Cash | 300 | 1500 |2017-05-05 |
fourth_table
---------------------------------------------------------------------------------------------------------------------------------------------------
| fid | vid | emp_id | expcal | exp1 | exp2 | exp3 | exp4 | exp5 | exp_amt | exp_desc | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | | | 2017-05-05 |
| 2 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | | | 2017-05-06 |
five_table
| fi_id | vid | emp_id | totkm | totamt | expenses | today_balance | handover_amt | balance_amt | handover_to | plstatus | entry_date | entry_time |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 10000 | 5000 | 100 | 4900 | 4000 | 400 | ram | PROFIT | 2017-05-05 | 04:35:21 |
| 2 | TN22BQ6226 | 3 | 1250 | 3500 | 100 | 3400 | 3000 | 200 | raj | PROFIT | 2017-05-06 | 04:36:58 |
shift
---------------------------------------------------------------------------------------------------------------------------------------------------
| sid | emp_id | vid | opeing_km | opeing_cash | closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | status |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | 1 |
| 2 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | 1 |
And i try this query i get wrong out,
SELECT a.vnumber
, a.vname
, b.eid
, b.name
, b.mobile
, c.tid
, c.vid
, c.emp_id
, c.pick_place
, c.start_time
, c.drop_place
, c.stop_time
, c.pickupkm
, c.drops
, c.type_of_trip
, c.travelkm
, c.tamt
, c.dates
, d.vid
, d.emp_id
, d.expcal
, d.exp1
, d.exp2
, d.exp3
, d.exp4
, d.exp5
, d.expamt
, d.expdesc
, d.dates
, e.emp_id
, e.vid
, e.opeing_km
, e.opeing_cash
, e.closing_km
, e.closing_cash
, e.opeing_date
, e.opeing_time
, e.closing_date
, e.closing_time
, f.vid
, f.emp_id
, f.totkm
, f.totamt
, f.expenses
, f.handover_amt
, f.balance_amt
, f.handover_to
, f.plstatus
, f.entry_date
FROM add_vehicle a
JOIN third_table c
ON a.vnumber = c.vid
JOIN add_employees b
ON b.eid = c.emp_id
JOIN fourth_table d
ON a.vnumber = d.vid
JOIN shift e
ON b.eid = e.emp_id
JOIN five_table f
ON a.vnumber = f.vid
, (SELECT #rownum := 0) r
WHERE c.type_of_trip IS NOT NULL
AND c.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND d.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND f.entry_date BETWEEN '2017-05-05' AND '2017-06-05'
AND c.vid = 'TN22BQ6226'
AND f.vid = 'TN22BQ6226'
AND e.vid = 'TN22BQ6226'
GROUP
BY c.tid
ORDER
BY c.dates
, f.entry_date DESC
i get fourth_table and five_table values are repeated like this.
one and two
I want the output like this,
| vnumber | vname | eid | name | tid | vid | emp_id | pick_place | start_time | drop_place | stop_time | pickup_km | drops | type_of_trip | travel_km | tamt | dates | vid | emp_id |expcal |exp1 | exp2 | exp3 | exp4 | exp5 | expamt| expdesc | dates | emp_id | vid | opening_km | opening_cash | closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | vid | emp_id | totkm | totamt | expenses | handover_amt | balance_amt| handover_to | plstatus | entry_date |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 1 | TN22BQ6226 | 3 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 2 | TN22BQ6226 | 3 | Hopes | 09:44 am | ESI | 12:01 pm | 1250 | 2500 | Cash | 1250 | 3500| 2017-05-06 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | 50 | |2017-05-06 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | TN22BQ6226 | 3 | 1250| 3500 | 100 | 3400 | 200 | raj | PROFIT | 2017-05-06 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 3 | TN22BQ6226 | 3 | place one | 03:15 pm | place two | 06:15 pm | 1500 | 1800 | Cash | 300 | 1500| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |
sorry for big question.
Too long for a comment...
Let's start by simplifying the problem as follows:
SELECT a.vid
, d.emp_id
, d.expcal
, f.entry_date
, f.entry_time
FROM add_vehicle a
JOIN fourth_table d
ON d.vid = a.vnumber
JOIN five_table f
ON f.vid = a.vnumber
vid emp_id expcal entry_date entry_time
1 3 100 2017-05-05 04:35:21
1 3 50 2017-05-05 04:35:21
1 3 100 2017-05-06 04:36:58
1 3 50 2017-05-06 04:36:58
http://sqlfiddle.com/#!9/62f3e5/21
Considering only the columns provided above, how does this differ from the desired result?
Incidentally - always store dates and times as a single entity
Related
I have two tables orders and customers:
select * from orders;
+------+---------------------+-------------+--------+
| oid | date | customer_id | amount |
+------+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 4 | 300 |
| 100 | 2009-10-08 00:00:00 | 3 | 15000 |
| 101 | 2008-10-08 00:00:00 | 2 | 1300 |
| 105 | 2010-10-08 00:00:00 | 1 | 400 |
| 106 | 2014-12-23 00:00:00 | 3 | 300 |
+------+---------------------+-------------+--------+
select * from customers;
+------+--------+------+-----------+----------+
| id | name | age | address | salary |
+------+--------+------+-----------+----------+
| 1 | ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | khilan | 25 | delhi | 1500.00 |
| 3 | muffy | 22 | bhopal | 8500.00 |
| 4 | suresh | 48 | mumbai | 24000.00 |
| 1 | ramesh | 32 | Ahmedabad | 300.00 |
| 5 | akil | 21 | madurai | 1000.00 |
| 6 | rajesh | 22 | delhi | 5000.00 |
+------+--------+------+-----------+----------+
What I'm trying to do is to do SUM(salary) from customers and subtract it with the SUM(amount) from orders table. I have tried with this query:
SELECT id ,NAME,SUM(salary),SUM(amount),SUM(salary)-SUM(amount)
FROM customers a LEFT JOIN orders b ON a.id=b.customer_id
GROUP BY NAME;
This will return the following result, which some of them return incorrect value:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | NULL |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 17000.00 | 15300 | 1700.00 |
| 6 | rajesh | 5000.00 | NULL | NULL |
| 1 | ramesh | 2300.00 | 800 | 1500.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
My expected output is as following:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | 1000 |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 8500 | 15300 | -6800 |
| 6 | rajesh | 5000.00 | NULL | 5000 |
| 1 | ramesh | 2300.00 | 400 | 1900.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
One way is to make calculation on orders into sub-query.
To cater for NULL value, you can use IFNULL(value,0).
SELECT id,NAME,SUM(salary),amt,SUM(salary)-IFNULL(amt,0)
FROM customers a LEFT JOIN
(SELECT customer_id, SUM(amount) amt FROM orders GROUP BY customer_id) b
ON a.id=b.customer_id
GROUP BY NAME;
I have 3 tables :
60.000 record Invoice
100.000 record Exchange Rate based on current date
25 record Currency Symbol
table_invoice:
+-----------+-------------+----------+--------------+
| invoice_id | currency_id | amount | invoice_date |
+------------+-------------+----------+--------------+
| 1 | 2 | 10 | 4/28/2016 |
| 2 | 3 | 30 | 4/29/2016 |
| 3 | 4 | 50 | 4/30/2016 |
| 4 | 2 | 40 | 6/18/2016 |
| 5 | 6 | 25 | 6/20/2016 |
| 6 | 7 | 87 | 6/25/2016 |
| 7 | 4 | 100 | 6/29/2016 |
| 8 | 9 | 45 | 7/14/2016 |
| 9 | 2 | 71 | 9/27/2016 |
| 60000 | 3 | 430 | 1/18/2017 |
+------------+-------------+----------+--------------+
and
table_exchange_rate:
+-----------------+-------------+---------------+--------------------+
| exchange_rate_id | currency_id | exchange_rate | exchange_rate_date |
+------------------+-------------+---------------+--------------------+
| 1 | 2 | 13.352 | 4/25/2016 |
| 2 | 3 | 10.195 | 4/25/2016 |
| 3 | 4 | 14.390 | 4/25/2016 |
| 4 | 5 | 1.720 | 4/25/2016 |
| 5 | 6 | 118 | 4/25/2016 |
| 6 | 7 | 9.468 | 4/25/2016 |
| 7 | 2 | 13.125 | 6/15/2016 |
| 8 | 3 | 10.520 | 6/25/2016 |
| 9 | 4 | 14.800 | 6/25/2016 |
| 10 | 5 | 1.800 | 6/25/2016 |
| 11 | 6 | 120 | 6/25/2016 |
| 12 | 7 | 9.320 | 6/25/2016 |
| 100000 | 7 | 9.500 | 6/25/2016 |
+------------------+-------------+---------------+--------------------+
and
reference_currency:
+-----------------+---------------+-----------------------+
| currency_id |currency_symbol| currency_name |
+------------------+---------------+-----------------------+
| 1 | USD | US Dollar |
| 2 | AUD | Australian Dollar |
| 3 | EUR | Euro |
| 4 | HKD | Hong Kong Dollar |
| 5 | JPY | Japan Yen |
| 6 | SGD | Singapore Dollar |
| 7 | MYR | Malaysian Ringgit |
| 8 | CHF | Swiss Franc |
| 9 | THB | Thailand Baht |
| 10 | GBP | Great Britain Pounds |
| 11 | SEK | Swedish Krona |
| 12 | CNY | China Yuan |
| 25 | SAR | Saudi Arabian Riyal |
+------------------+-------------+-------------------------+
When I run:
SELECT
a.invoice_id AS 'INVOICE_ID',
a.currency_id AS 'CURRENCY_ID',
a.amount AS 'AMOUNT',
a.invoice_date AS 'INVOICE_DATE',
(
SELECT b.exchange_rate FROM table_exchange_rate b
WHERE b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id
ORDER BY b.exchange_rate_date DESC LIMIT 1) AS 'EXCHANGE RATE'
)
FROM table_invoice a
Result:
`+------------+-------------+----------+--------------+--------------+
| INVOICE_ID | CURRENCY_ID | AMOUNT | INVOICE_DATE | EXCHANGE_RATE |
+------------+-------------+----------+--------------+---------------+
| 1 | 2 | 10 | 4/28/2016 | 13.352
| 2 | 3 | 30 | 4/29/2016 | 10.195
| 3 | 4 | 50 | 4/30/2016 | 14.390
| 4 | 2 | 40 | 6/18/2016 | 13.125
| 5 | 6 | 25 | 6/20/2016 | 118
| 6 | 7 | 87 | 6/25/2016 | 9.320
`
and the result works fine but it is very slow(approximately > 60sec) with 60k record(table_invoice) looping on 100k record(table_exchange_rate) finding exchange rate at the current date,
If invoice_date can't match with exchange_rate_date or the user doesn't input the exchange rate on the app, it will using exchange rate the latest record already in input before the current date (b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id)
Can I speed up this query or there any other options? Thank you..
Try same thing with JOIN
select a.invoice 'invoice_id', a.currency_id 'curency_id', a.amount 'amount', a.invoice_date 'invoice_date', ter.exchange_rate ' exchange_rate'
from table_invoice ti
left join table_exchange_rate ter on ter.currency_id = ti.currency_id and ter.exchange_rate_date <= ti.invoice_date
I have the following tables:
clients:
| id | name | code | zone |
--------------------------------
| 1 | client 1 | a1b1 | zone1|
| 2 | client 2 | a2b2 | zone2|
contacts:
| id_contact | first_name | last_name |
----------------------------------------
| 11 | first1 | last1 |
| 22 | first2 | last2 |
| 33 | first3 | last3 |
| 44 | first4 | last4 |
client_contacts:
| id_client | id_contact |
--------------------------
| 1 | 11 |
| 1 | 22 |
| 1 | 33 |
| 2 | 11 |
| 2 | 44 |
offers:
| id_offer | id_client | value |
--------------------------
| 111 | 1 | 100 |
| 222 | 1 | 200 |
| 333 | 1 | 300 |
| 444 | 2 | 400 |
I would like through a optimal select to obtain:
| id_client | name | code | zone | contacts_pers | total_offer_value |
----------------------------------------------------------------------------
| 1 | client 1 | a1b1 | zone1 | first1 last1; | 600 |
first2 last2;
first3 last3;
| 2 | client 2 | a2b2 | zone2 | first1 last1; | 400 |
first4 last4;
I know how to get the desired result with "group_concat" and stored procedures for "total_offer_value". But how to get the desired result from a single efficient select?
SELECT c.id, c.name, c.code, c.zone, GROUP_CONCAT(DISTINCT CONCAT(co.first_name, " ", c.last_name) SEPARATOR ";") AS contact_pers, func_total_offer_value(c.id) AS total_offer_value
FROM clients c
LEFT OUTER JOIN (client_contacts cc, contacts co) ON ( c.id = cc.id_client AND cc.id_contact = co.id_contact )
GROUP BY c.id
I have two tables (champ_value and champ_form), i tried two queries but i didn't obtain the result that I want,
please can you give me other solution
Thanks in advance.
+-------------+-----------+---------------------+---------------+
| champ value | | | |
| | | | |
| v_id | v_value | v_fk_order_item_id | v_fk_champ_id |
| 220 | Bernad | 20000 | 1 |
| 221 | Lagaf | 20000 | 2 |
| 500 | Vincent | 20000 | 1 |
| 501 | Pernault | 20000 | 2 |
+-------------+-----------+---------------------+---------------+
+------------+-------------+---------------+-------------+
| champ_form | | | |
| cf_id | cf_position | cf_fk_form_id | cf_champ_id |
| 330 | 10 | 1800 | 1 |
| 331 | 12 | 1800 | 2 |
| 630 | 13 | 1800 | 1 |
| 631 | 14 | 1800 | 2 |
+------------+-------------+---------------+-------------+
the desired result will be like this :
+----------------+-------+-------------+-------------+----------+
| desired result | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 331 | 12 | 2 | Lagaf |
| 500 | 630 | 13 | 1 | Vincent |
| 501 | 631 | 14 | 2 | Pernault |
+----------------+-------+-------------+-------------+----------+
I tried this first query :
SELECT v.v_id, cf.cf_id, cf.cf_position, cf.cf_champ_id, v.v_value
FROM champ_form cf
JOIN champ_value v ON v.v_fk_champ_id = cf.cf_champ_id
WHERE cf.cf_fk_form_id =1800
AND v.v_fk_order_item_id =20000
GROUP BY v_id
ORDER BY cf.cf_position
and I obtain this :
+---------------+-------+-------------+-------------+----------+
| group by v_id | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 330 | 10 | 1 | Vincent |
| 500 | 331 | 12 | 2 | Lagaf |
| 501 | 331 | 12 | 2 | Pernault |
+---------------+-------+-------------+-------------+----------+
and the second query :
SELECT v.v_id, cf.cf_id, cf.cf_position, cf.cf_champ_id, v.v_value
FROM champ_form cf
JOIN champ_value v ON v.v_fk_champ_id = cf.cf_champ_id
WHERE cf.cf_fk_form_id =1800
AND v.v_fk_order_item_id =20000
GROUP BY cf.cf_id
ORDER BY cf.cf_position
+----------------+-------+-------------+-------------+---------+
| group by cf_id | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 331 | 12 | 2 | Lagaf |
| 220 | 630 | 13 | 1 | Bernard |
| 221 | 631 | 14 | 2 | Lagaf |
+----------------+-------+-------------+-------------+---------+
The first query gives the good values but not the correct positions
and the second gives the correct positions but not the correct values.
Without describing the desired behavior is difficult to understand what you want. But probably the problem is that the selected columns are not in the GROUP BY.
Try the following:
SELECT aa.v_id, cc.cf_id, cc.cf_position, cc.cf_champ_id, aa.v_value
FROM champ_value AS aa
INNER JOIN (
SELECT _aa.v_id
FROM champ_value AS _aa
INNER JOIN champ_form AS _bb
ON _aa.v_fk_champ_id = _bb.cf_champ_id
WHERE _aa.cf_fk_form_id = 1800 AND _bb.v_fk_order_item_id = 20000
GROUP BY _aa.v_id
) AS bb
ON aa.v_id = bb.v_id
INNER JOIN champ_form AS cc
ON aa.v_fk_champ_id = cc.cf_champ_id
ORDER BY aa.cf_position
More info here and here.
This does display according to position - still not convinced it's safe though!
/*
create table champ_value ( v_id int, v_value varchar(10), v_fk_order_item_id int, v_fk_champ_id int);
truncate table champ_value;
insert into champ_value values
(220 , 'Bernad' , 20000 , 1 ),
(221 , 'Lagaf' , 20000 , 2 ),
(500 , 'Vincent' , 20000 , 1 ),
(501 , 'Pernault' , 20000 , 2 );
create table champ_form(cf_id int,cf_position int, cf_fk_form_id int ,cf_champ_id int);
insert into champ_form values
(330 , 10 , 1800 , 1 ),
(331 , 12 , 1800 , 2 ),
(630 , 13 , 1800 , 1 ),
(631 , 14 , 1800 , 2 );
+----------------+-------+-------------+-------------+----------+
| desired result | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 331 | 12 | 2 | Lagaf |
| 500 | 630 | 13 | 1 | Vincent |
| 501 | 631 | 14 | 2 | Pernault |
+----------------+-------+-------------+-------------+----------+
*/
SELECT s.v_id,t.cf_id, t.cf_position ,t.cf_champ_id ,s.v_value
FROM
(
SELECT I.v_id,v_value,v_fk_order_item_id,v_fk_champ_id,
#RN:=#RN + 1 RN
FROM (SELECT #RN:=0) RN,champ_value I
order by i.v_id asc
) S
LEFT OUTER JOIN
(SELECT cf_id,cf_position, cf_fk_form_id,cf_champ_id,
#RN1:=#RN1 + 1 RN1
FROM (SELECT #RN1:=0) RN1, champ_form E
order by e.cf_id asc
) T ON T.RN1 = S.RN
Actual Result
+------+-------+-------------+-------------+----------+
| v_id | cf_id | cf_position | cf_champ_id | v_value |
+------+-------+-------------+-------------+----------+
| 220 | 330 | 10 | 1 | Bernad |
| 221 | 331 | 12 | 2 | Lagaf |
| 500 | 630 | 13 | 1 | Vincent |
| 501 | 631 | 14 | 2 | Pernault |
+------+-------+-------------+-------------+----------+
I have this working query that generate data the way i like it to happened but now i want it to add another clause statement that will get rows from tbl_loan_master that was not on the tbl_loanledger.
Example Data:
tbl_borrowers
------------------------------------------
| id | first_name | last_name | deleted |
| 1 | Joe | Smith | 0 |
| 2 | Lily | Mag | 0 |
| 3 | Zen | Green | 0 |
| 4 | Kim | Chan | 0 |
| 5 | Bob | Mac | 1 |
| 6 | Ben | Cork | 0 |
------------------------------------------
tbl_loan_master
----------------------------------------------------------------------
| id | borrowers_id | loan | date_created | due_date | deleted |
| 1 | 4 | 300 | 2016/04/28 | 2017/04/28 | 0 |
| 2 | 1 | 100 | 2016/05/05 | 2017/05/05 | 0 |
| 3 | 2 | 500 | 2016/06/08 | 2017/06/08 | 0 |
| 4 | 1 | 200 | 2016/06/13 | 2017/06/13 | 0 |
| 5 | 3 | 150 | 2016/06/15 | 2017/06/15 | 0 |
| 6 | 6 | 50 | 2016/06/16 | 2017/06/16 | 0 |
----------------------------------------------------------------------
tbl_loanledger
------------------------------------------------------------------------------
| id | borrowers_id | loanmaster_id | payment | balance| date_created | deleted
| 1 | 4 | 1 | 50 | 250 | 2016/05/28 | 0
| 2 | 1 | 2 | 20 | 80 | 2016/05/25 | 0
| 3 | 1 | 2 | 30 | 50 | 2016/06/01 | 0
| 4 | 2 | 3 | 100 | 400 | 2016/06/09 | 0
| 5 | 2 | 3 | 50 | 350 | 2016/06/10 | 0
| 6 | 3 | 4 | 50 | 150 | 2016/06/16 | 0
------------------------------------------------------------------------------
Here is the working query:
$query = "SELECT `tbl_borrowers`.* , `tbl_loanledger`.*, `tbl_loan_master`.*
FROM `tbl_borrowers`
LEFT JOIN `tbl_loanledger`
ON `tbl_borrowers`.id = `tbl_loanledger`.borrower_id
LEFT JOIN `tbl_loan_master`
ON `tbl_loan_master`.id = `tbl_loanledger`.loanmaster_id
WHERE `tbl_borrowers`.deleted = 0 AND `tbl_loanledger`.deleted = 0 AND MONTH ( `tbl_loanledger`.date_created) = MONTH(CURRENT_DATE)
GROUP BY `tbl_loanledger`.borrower_id
ORDER BY `tbl_borrowers`.last_name";
The expected result will output the borrowers with their last transaction on the current month (which is June) in loan ledger and also in loans accounts. Like Ben Cork which is not on the loan ledger, he is on the loan accounts but i want to output him on the result set. The deleted column means if it is 0 it indicates it is active if it is 0 it means it has been deleted.
Expected Result:
|First Name | Last Name | Due Date | Balance |
| Ben | Cork | 2017/06/16 | 50 |
| Joe | Smith | 2017/06/13 | 50 |
| Lily | Mag | 2017/06/08 | 350 |
| Zen | Green | 2017/06/15 | 150 |
Try following;)
select
tb.first_name, tb.last_name, coalesce(tlm.Loan, 0) as Loan, coalesce(t.`Amount Paid`, 0) as `Last Amount Paid`
from tbl_borrowers tb
left join tbl_loan_master tlm
on tb.id = tlm.borrowers_id
left join (
select t1.*
from tbl_loanledger t1
inner join (
select max(id) as id
from tbl_loanledger
group by borrowers_id, loanmaster_id
) t2 on t1.id = t2.id
) t
on tb.id = t.borrowers_id
and tlm.id = t.loanmaster_id
SQLFiddle DEMO HERE