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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路