$sql="SELECT month(date) AS mon_th , sum(money) AS Total_bg1 FROM data where idaccount='1' and idsubject='$idsubject' and idtype='$idtype' GROUP BY month(date)";
$table=mysql_query($sql,$Conn) or die ("ไม่สามารถเรียกดูข้อมูลในเทเบิล ได้");
//รายจ่าย
//$sql="SELECT sum(money) AS Total_bg2 FROM data where idaccount='2' and idsubject='$idsubject' and idtype='$idtype' GROUP BY month(date)";
//$table1=mysql_query($sql1,$Conn) or die ("ไม่สามารถเรียกดูข้อมูลในเทเบิล ได้2");
//$row = mysql_fetch_array($table1);
//$Total_bg2 = $row['Total_bg2'];
$number=mysql_num_rows($table);
$no=1;
if($number<>0){
echo"
<p><strong>รายการ</strong></p>
<table border=1>
<tr BGCOLOR=#E8E8E8>
<td><center><b>ลำดับที่</b></center></td>
<td><center><b>เดือน</b></center></td>
<td><center><b>รายรับ</b></center></td>
<td><center><b>รายจ่าย</b></center></td>
<td><center><b>คงเหลือ</b></center></td>
</tr>";
//while($row=mysql_fetch_array($table1)){
//$total_bg2 = $row["Total_bg2"];
while($row=mysql_fetch_array($table)){
$iddata=$row[iddata];
$mon_th=$row[mon_th];
$money=$row[money];
$note=$row[note];
$idtype=$row[idtype];
$idaccount=$row[idaccount];
$total_bg1 = $row["Total_bg1"];
echo "
<tr>
<td>$no</td>
<td>$mon_th</td>
<td>$total_bg1</td>
<td>$Total_bg2</td>
<td>--- </td>
</tr>";
$no++;
}
select a.mon_th, a.Total_bg1, b.Total_bg2 from (
SELECT month(date) AS mon_th , sum(money) AS Total_bg1 FROM data where idaccount='1' and idsubject='$idsubject' and idtype='$idtype' GROUP BY month(date)
) a left join (
SELECT month(date) AS mon_th, sum(money) AS Total_bg2 FROM data where idaccount='2' and idsubject='$idsubject' and idtype='$idtype' GROUP BY month(date)
) b on a.mon_th = b.mon_th order by a.mon_th