ds1379551 2015-12-26 15:07
浏览 37
已采纳

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

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());'
  • 写回答

1条回答 默认 最新

  • dongne1560 2015-12-26 21:13
    关注

    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";}}}"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line