简体   繁体   中英

PHP Access SQL help on round up values

Please help me on sql queries for round up the value after fetching from Access database. I'm working on generating mark-sheet using php and access. But can't round up the values on a single query. I'm trying to add values but this supposed to be round up first before the addition. I mean if marks field contain 14.5 then should be 15 before adding but not converting the actual database value. Here is my code...

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 doesn't have a CEILING function, and without having access to a VBA function, you could use some maths and the Int function.

Try using this code. I've used this logic to determine if it should round up or not. It assumes that marks is always positive:

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

Also, I've formatted the SQL code to make it easier to read and maintain.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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