I have two table Table A and Table B in mysql Database
Table A
id date advanced payed_remaining remaining_date
1 1/1/2018 400 800 4/1/2018
2 2/1/2018 600 600 3/1/2018
3 4/1/2018 800 200 6/1/2018
4 6/1/2018 400 300 8/1/2018
5 3/2/2018 600 200 6/2/2018
6 8/2/2018 800 400 10/2/2018
Table B
id date amount
1 1/1/2018 900
2 2/1/2018 600
3 4/1/2018 300
4 2/2/2018 400
5 5/2/2018 800
Query for fetching monthly Data from both table,
$monthly_res = $con->prepare("SELECT t2.month, t2.total_advance, t2.total_pay_remaining, t1.total_income_amount FROM (SELECT month(B.date) as month, SUM(B.income_amount) as total_income_amount FROM B group by month(B.date)) t1 INNER JOIN (SELECT month(A.date) as month, month(A.due_date) as month, t1.total_income_amount, SUM(A.advance) as total_advance, SUM(A.pay_remaining) as total_pay_remaining, t1.total_income_amount FROM A) t2 ON t2.month = t1.month");
$monthly_res->execute();
while ($row = $monthly_res->fetch(PDO::FETCH_ASSOC)) {
$month = $row['month'];
$dt = DateTime::createFromFormat('!m', $month);
$month_name = $dt->format('F');
$total = $row['total_advance'] + $row['total_income_amount'] + $row['total_pay'];
echo "<tbody>
<tr>
<td>".$month_name."</td>
<td>".$total."/-</td>
</tr>
</tbody>";
}
1054 - Unknown column 't1.total_income_amount' in 'field list'
when i remove t1.total_income_amount from query its showing December result only...
I want fetch monthly sum of advanced, sum of payed_remaining, sum of amount in one loop.
Result= sum(advanced) + sum(payed_remaining) + sum(amount) by month
January : 5900
February : 3200