order by子句打破了Play 2.1.2中的Anorm选择查询

时间:2014-02-15 19:32:42

标签: mysql anorm

我在Play 2.1.2中有以下代码

def list(startDate:String, endDate:String,  page:Option[Int], pageSize:Option[Int] ) = {
    DB.withConnection { implicit connection =>
        val c = SQL("""
              select 
                employeeId,
                CertType, 
                ct.name, 
                applicable,
                due,
                year(due) as yeardue,   month(due) as monthdue, day(due) as daydue, 
                year(completed),     month(completed),     day(completed), 
                year(last),       month(last),       day(last), 
                status 
              from cert 
              join certtype ct on CertType=ct.code
              where due<{endDate} and due>{startDate} and CertType not in (500,600)
              order by due
            """ 
          )
          .on('index -> page, 'pageSize -> pageSize , 'startDate -> startDate, 'endDate->endDate)().collect{
            case Row(  
                  employeeId:Long,
                  certType:Int, 
                  name:String, 
                  applicable:Int,
                  Some(due:java.sql.Date),
                  Some(yeardue:Long),      Some(monthdue:Long),     Some(daydue:Long),
                  Some(yearcompleted:Long),  Some(monthcompleted:Long),   Some(daycompleted:Long),
                  Some(yearlast:Long),    Some(monthlast:Long),     Some(daylast:Long),
                  status:Long) =>
                  Reminder(
                      employeeId, 
                      certType, 
                      name,  
                      if (applicable>0) true else false, 
                      makeDate(yeardue,    monthdue,    daydue),  
                      makeDate(yearcompleted,  monthcompleted,  daycompleted),  
                      makeDate(yearlast,    monthlast,    daylast),  
                      status);
        }
        c.toList
    }
}

这种方法的唯一方法是删除'order by'子句 Order by仅适用于像ct.name这样的字符串字段。 否则,列表为空。

我没有此消息,只是一个空白列表。

1 个答案:

答案 0 :(得分:0)

好吧,现在这个有效,我不知道为什么。

这是工作版本:

def list(startDate:String, endDate:String,  page:Option[Int], pageSize:Option[Int] ) = {
    DB.withConnection { implicit connection =>
        val c = SQL("""
                        select 
                            employeeId,
                            CertType, 
                            ct.name, 
                            applicable,
                            due,
                            completed,
                            last,
                            status 
                        from cert 
                        join certtype ct on CertType=ct.code
                        where due between {startDate} and {endDate} and CertType not in (500,600)
                        order by due
                    """ 
            )
            .on('index -> page, 'pageSize -> pageSize , 'startDate -> startDate, 'endDate->endDate)().collect{
                case Row(   
                            employeeId:Long,
                            certType:Int, 
                            name:String, 
                            applicable:Int,
                            Some(due:java.sql.Date),        // optional to handle potential null value
                            Some(completed:java.sql.Date),  // optional to handle potential null value
                            last:java.sql.Date,             // this is probably required to NOT be optional since it is a PK in the schema (?)
                            status:Long) =>
                    Reminder(
                            employeeId, 
                            certType, 
                            name,  
                            if (applicable>0) true else false, 
                            new DateTime(due.       getTime()).toString("yyyy-MM-dd"),
                            new DateTime(completed. getTime()).toString("yyyy-MM-dd"),
                            new DateTime(last.      getTime()).toString("yyyy-MM-dd"),
                            status);
        }
      c.toList
    }
  }