PHP访问SQL有关舍入值的帮助

时间:2015-12-26 15:07:59

标签: php sql odbc

从Access数据库中获取后,请帮我解决sql查询的问题。我正致力于使用php和访问生成标记表。但是不能在单个查询上舍入值。我试图添加值,但这应该在添加之前先进行舍入。我的意思是如果标记字段包含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());'

1 个答案:

答案 0 :(得分:0)

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";}}}"