|
|
|
ช่วยผมที่ครับ คือผมต้องการหาผลรวมบวก ของ database ที่ผม search เจอครับ |
|
|
|
|
|
|
|
ใช้ sum() ครับ
ตัวอย่าง Code (SQL)
select sum(your_field) as total from your_table where 1
|
|
|
|
|
Date :
2013-10-05 11:37:12 |
By :
mangkunzo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เอาโค้ดเต็มมาลงดีกว่าฟระ
|
|
|
|
|
Date :
2013-10-05 14:04:25 |
By :
แแแ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<?php require_once('Connections/dataoil.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
$colname_Recordset1 = "-1";
if (isset($_POST['name'])) {
$colname_Recordset1 = $_POST['name'];
}
mysql_select_db($database_dataoil, $dataoil);
$query_Recordset1 = sprintf("SELECT * FROM oilkamal WHERE name LIKE %s", GetSQLValueString("%" . $colname_Recordset1 . "%", "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $dataoil) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="1088" height="463" border="1" id="mytable">
<tr>
<td colspan="10" bgcolor="#99FF00"><div align="center"> <strong> เติมเลิศวรสมล </strong></div></td>
</tr>
<tr>
<td colspan="3"> </td>
<td colspan="2"><div align="center"> <strong> เลขที่ไมล์ </strong></div></td>
<td colspan="5"><label for="fine"></label>
<input type="text" name="fine" id="fine" />
<input type="submit" name="fine2" id="fine2" value="ค้นหา" /></td>
</tr>
<tr>
<td width="100" height="26"><div align="center"> <strong> วัน/เดือน/ปี </strong></div></td>
<td width="97"><div align="center"> <strong> ทะเบียน </strong></div></td>
<td width="111"><div align="center"> <strong> ผู้ใช้ </strong></div></td>
<td width="104"><div align="center"><strong>ผู้อนุมัติ</strong></div></td>
<td width="81"><div align="center"><strong>เริ่ม</strong></div></td>
<td width="84"><div align="center"> <strong> ปริมาณ </strong></div></td>
<td width="80"><div align="center"> <strong> ลิตร/บาท </strong></div></td>
<td width="55"><div align="center"> <strong> จำนวนเงิน </strong></div></td>
<td width="208"><div align="center"><strong>หมายเหต</strong>ุ</div></td>
<td width="104"> </td>
</tr>
<?php do { ?>
<tr>
<td height="26"><div align="center"><?php echo $row_Recordset1['date']; ?></div></td>
<td><div align="center"><?php echo $row_Recordset1['register']; ?></div></td>
<td><div align="center"><?php echo $row_Recordset1['name']; ?></div></td>
<td><div align="center"><?php echo $row_Recordset1['end']; ?></div></td>
<td><div align="center"><?php echo $row_Recordset1['start']; ?></div></td>
<td><div align="center"><?php echo $row_Recordset1['liters']; ?></div></td>
<td><div align="center"><?php echo $row_Recordset1['tatal']; ?></div></td>
<td bgcolor="#99FF00"><div align="center"><strong>
<?=$row_oil_kamal['liters']*$row_oil_kamal['tatal'];?>
</strong></div></td>
<td bgcolor="#CCCCCC"> </td>
<td bgcolor="#FFCC00"> </td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<tr>
<td height="26" colspan="5"><div align="right"><strong><em>รวม</em></strong></div>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div>
<div align="center"></div></td>
<td bgcolor="#CCFF00">
<td bgcolor="#CCFF00"><?php
$query="SELECT sum(tatal) cnt FROM oilkamal";
$result=mysql_query($query);
$row=mysql_fetch_assoc($result);
echo $row['cnt'];;
?></td>
<td bgcolor="#00FF00"> </td>
<td bgcolor="#FFFFFF"> </td>
<td bgcolor="#FFFF00"> </td>
</tr>
<tr>
<td height="131" colspan="10"><div align="center">
<p><strong><a href="kamol.php">เพิ่ม</a></strong></p>
<table border="0">
<tr>
<td><img src="image/First.gif" alt="" /></td>
<td><img src="image/Previous.gif" alt="" /></td>
<td><img src="image/Next.gif" alt="" /></td>
<td><img src="image/Last.gif" alt="" /></td>
</tr>
</table>
<em><strong><a href="main.php">กลับหน้าหลัก
</p>
</a></strong></em><a href="main.php"></a></div></td>
</tr>
<tr>
<td height="26" colspan="10"><div align="center"><img src="image/logooil.jpg" alt="" width="325" height="181" /></div></td>
</tr>
</table>
<div align="center"><br />
<a href="search.php">ค้นหา</a> </div>
</form>
<div align="center"></div>
<p> </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
|
|
|
|
|
Date :
2013-10-05 14:22:44 |
By :
dRufFy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$query="SELECT sum(tatal) cnt FROM oilkamal";
ไม่ใส่เงื่อนไข where มันก็คำนวณหมดสิครับ
|
|
|
|
|
Date :
2013-10-05 14:58:57 |
By :
สบายดี |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เหลือแค่ sum ค่าที่ค้นหามาได้ เฉพาะข้อมูลที่ค้นเจอ เช่นผมค้นหาวันที่ 2 ถึงวันที่ 5 เอาแค่ผลรวมของวันที่ 2-5 ครับ เหลือแค่อันนี้อันเดียวครับ รบกวนอีกทีนะครับ ขอบคุงครับ ปล.ขอตัวอย่างด้วยนะครับ อิๆๆ
|
|
|
|
|
Date :
2013-10-05 16:01:23 |
By :
dRufFy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|