繁体   English   中英

PHP Access SQL帮助汇总值

[英]PHP Access SQL help on round up values

从Access数据库中获取值后,请帮助我进行sql查询以求取整值。 我正在使用php和access生成标记表。 但是不能在单个查询中将值四舍五入。 我正在尝试添加值,但这应该在添加之前先进行四舍五入。 我的意思是,如果标记字段包含14.5,则在添加但不转换实际数据库值之前应为15。 这是我的代码...

if(isset($_GET['action']) && $_GET['action']=='rlist'){
 if($_GET['element_1']=="IX"){
   if( $_GET['element_2']!=1){
   $sql="select m.StudentId,StudentName, m.Roll,m.Sec,m.P1,m.P2,m.P3,m.S1,m.S2,m.S3,m.Total from StudentMain right join(select StudentId, Roll, Sec,Sum(iif(ExamType=4,Marks,0)) as P1,Sum(iif(ExamType=5,Marks,0)) as P2,Sum(iif(ExamType=6,Marks,0)) as P3, Sum(iif(ExamType=1,Marks,0)) as S1,Sum(iif(ExamType=2,Marks,0)) as S2,Sum(iif(ExamType=3,Marks,0)) as S3, (S1+S2+S3+P1+P2+P3) as Total from Marks where Class='".$_GET['element_1']."' and Sec='".$_GET['element_2']."'group by StudentId,Roll,Sec) as m on StudentMain.StudentId=m.StudentId order by m.Roll";}}}


$rs = odbc_exec($conn,$sql)or die (print odbc_errormsg());'

Access没有CEILING函数,并且没有访问VBA函数的权限,您可以使用一些数学运算和Int函数。

尝试使用此代码。 我已经使用此逻辑来确定是否应四舍五入。 它假定标记始终为正:

Int(Marks)+IIf(Marks-Int(Marks)>0,1,0)

另外,我对SQL代码进行了格式化,以使其更易于阅读和维护。

$sql="select m.StudentId,StudentName, m.Roll,m.Sec,m.P1,m.P2,m.P3,m.S1,m.S2,m.S3,m.Total " .
"from StudentMain " .
"right join( " .
    "select StudentId, Roll, Sec,Sum(iif(ExamType=4,Marks,0)) as P1, " .
    "Sum(iif(ExamType=5,Int(Marks)+IIf(Marks-Int(Marks)>0,1,0),0)) as P2, " .
    "Sum(iif(ExamType=6,Int(Marks)+IIf(Marks-Int(Marks)>0,1,0),0)) as P3, " .
    "Sum(iif(ExamType=1,Int(Marks)+IIf(Marks-Int(Marks)>0,1,0),0)) as S1, " .
    "Sum(iif(ExamType=2,Int(Marks)+IIf(Marks-Int(Marks)>0,1,0),0)) as S2, " .
    "Sum(iif(ExamType=3,Int(Marks)+IIf(Marks-Int(Marks)>0,1,0),0)) as S3, " .
    "(S1+S2+S3+P1+P2+P3) as Total  " .
"from Marks w " .
"here Class='".$_GET['element_1']."'  " .
"and Sec='".$_GET['element_2']."' " .
"group by StudentId,Roll,Sec) as m  " .
"on StudentMain.StudentId=m.StudentId order by m.Roll";}}}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM