|
 |
|
สอบถามการ sum ข้อมูลใน SQL มาแสดงครับ รบกวนหน่อยนะครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
<?
session_start();
if (session_is_registered("valid_uname") && session_is_registered("valid_pwd")) {
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
<!--
.style1 {
font-family: "TH SarabunPSK";
font-size: 18pt;
font-weight: bold;
}
.style2 {
font-family: "TH SarabunPSK";
font-size: 16pt;
font-weight: bold;
}
.style5 {cursor: hand; font-weight: normal; color: #000000;}
.style9 {font-family: Tahoma; font-size: 12px; }
.style11 {font-size: 12px}
.style13 {font-size: 9}
.style16 {font-size: 9; font-weight: bold; }
.style17 {font-size: 12px; font-weight: bold; }
-->
</style>
<?
require('connect.php');
?>
<form name="form1" action="<?php $_SERVER['PHP_SELF']?>">
<table align="center" width="200" border="0">
<tr>
<td><? require("head.php");?></td>
</tr>
<tr>
<td bgcolor="#F8F8F8" align="center"><table align="center"width="793" height="193" border="1" bgcolor="#F8F8F8">
<tr>
<td class="style2"width="120" height="48">รายการปีงบประมาณ</td>
<td class="style2" colspan="5">ปีงบประมาณ
<select name="datebg" onChange="this.form.submit()">
<option value="">เลือก</option>
<?php
$sql = "SELECT * FROM tbbudgetyear";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
if($datebg == $row['BudgetyearID']){ $sel="selected"; }else{ $sel=""; }
?>
<option value="<?=$row['BudgetyearID']?>" <?=$sel;?>><?=$row['Budgetyear']?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td height="28" align="center" class="style2">รหัส</td>
<td height="28" colspan="2" align="center" class="style2">หมวด</td>
<td width="170" align="center" class="style2">จำนวนเงินที่ได้รับจัดสรร</td>
<td width="213" align="center" class="style2">จำนวนเงินที่ใช้จริง</td>
<td width="141" colspan="2" align="center" class="style2">จำนวนเงินคงเหลือ</td>
</tr>
<?php
$sql_show = "SELECT * FROM tbbudgetcategory";
$sql_show .= " LEFT JOIN tbbudgettype ON tbbudgetcategory.BudgettypeID = tbbudgettype.BudgettypeID";
$sql_show .= " WHERE tbbudgetcategory.BudgetyearID = '".$datebg."'";
$result_show = mysql_query($sql_show) or die(mysql_error());
if(mysql_numrows($result_show) > 0){ // เช็คว่ามีข้อมูลหรือไม่
while($row_show = mysql_fetch_array($result_show)) {
?>
<tr class="style2">
<td height="24" align="center" class="style2"><?=$row_show['BudgetcategoryID']; ?></td>
<td height="23" colspan="2" align="center"><?=$row_show['Budgettype']; ?></td>
<td align="center"><?=number_format($row_show['Budgetcategoryamount'],2,'.',',');?></td>
<td align="center">
<?=number_format($row_show['Totalbill'],2,'.',',');?></td>
<td colspan="2" align="center"><?=number_format($row_show['$Budgetcategoryamount=$Budgetcategoryamount-$Totalbill'],2,'.',',');?></td>
</tr>
<?php } } else { ?>
<tr align="center">
<td colspan="9" class="style2">ไม่พบข้อมูลในปีงบประมาณนี้</td>
</tr>
<?php } ?>
<tr>
<td height="82" colspan="6" align="center"><img src="picture/back.gif" alt="" width="246" height="78" /></td>
</tr>
</table></td>
</tr>
<tr>
<td><? require ('footer.php')?></td>
</tr>
</table>
</form>
<?
}
else{
echo"<script language='JavaScript'>";
echo"alert('Please Login');";
echo"</script>";
echo "<meta http-equiv='refresh' content='0;url=index.php' />";
exit();
}
?>
คือผมอยากได้ผลลัพธ์ออกมาเป็นแบบนี้ โดยที่แยกตามหมวดหมู่ และ ปีงบประมาณครับ

นี่ฐานข้อมูลครับ
ฐานข้อมูลการจัดสรรงบตามหมวดหมู่

ฐานข้อมูลลการเบิกจ่ายงบ

ฐานข้อมูลหมวดหมู่

ฐานข้อมูลการจัดสรรงบแต่ละปี

Tag : PHP

|
ประวัติการแก้ไข 2014-07-25 13:43:46
|
 |
 |
 |
 |
Date :
2014-07-25 13:42:34 |
By :
bunditnu |
View :
845 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดาเอาว่า จำนวนเงินที่ใช้จริง มาจาก Clearbillpay ที่มี ButgettypeId เหมือนกัน
จับมันมาบวกกันก่อน แล้วเอาไปลบ Butgetcategoryamount มั๊งงงงง 
Code (PHP)
<tr>
<td height="28" align="center" class="style2">รหัส</td>
<td height="28" colspan="2" align="center" class="style2">หมวด</td>
<td width="170" align="center" class="style2">จำนวนเงินที่ได้รับจัดสรร</td>
<td width="213" align="center" class="style2">จำนวนเงินที่ใช้จริง</td>
<td width="141" align="center" class="style2">จำนวนเงินคงเหลือ</td>
</tr>
<?php
$sql_show = "SELECT * FROM tbbudgetcategory";
$sql_show .= " LEFT JOIN tbbudgettype ON tbbudgetcategory.BudgettypeID = tbbudgettype.BudgettypeID";
$sql_show .= " WHERE tbbudgetcategory.BudgetyearID = '".$datebg."'";
$result_show = mysql_query($sql_show) or die(mysql_error());
if(mysql_numrows($result_show) > 0){ // เช็คว่ามีข้อมูลหรือไม่
while($row_show = mysql_fetch_array($result_show)) {
$sql_sum = "SELECT SUM(Clearbillpay) AS SumClearbillpay FROM tbclearbill";
$sql_sum .= " WHERE BudgettypeID = '".$row_show['BudgettypeID']."'";
$result_sum = mysql_query($sql_sum) or die(mysql_error());
$row_sum = mysql_fetch_array($result_sum);
$Balance = $row_show['Budgetcategoryamount'] - $row_sum['SumClearbillpay'];
?>
<tr class="style2">
<td height="24" align="center" class="style2"><?=$row_show['BudgetcategoryID'];?></td>
<td height="23" colspan="2" align="center"><?=$row_show['Budgettype'];?></td>
<td align="center"><?=number_format($row_show['Budgetcategoryamount'],2,'.',',');?></td>
<td align="center"><?=number_format($row_sum['SumClearbillpay'],2,'.',',');?></td>
<td align="center"><?=number_format($Balance,2,'.',',');?></td>
</tr>
|
 |
 |
 |
 |
Date :
2014-07-25 14:58:30 |
By :
sabaitip |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2014-07-26 08:05:16 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|