【问题标题】:order by clause is breaking Anorm select query in Play 2.1.2order by 子句打破了 Play 2.1.2 中的异常选择查询
【发布时间】:2014-02-15 19:32:42
【问题描述】:

我在 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 这样的字符串字段。 否则,列表为空。

我没有由此产生的错误消息,只是一个空白列表。

【问题讨论】:

  • 从效率的角度来看,实际上在代码中排序通常比在数据库中排序更受欢迎。此外,按 SQL 顺序排列的 AFAIK 也适用于日期。可能是 Anorm 的问题?你能用 Slick 代替吗?或者只是不要在数据库中订购。此外,due between startDate and endDate 的效率也稍高一些。
  • 嗯...不确定在代码中排序是否更有效,但感谢您提供有关使用“介于”作为日期范围的提示。

标签: mysql anorm


【解决方案1】:

好的,现在可以了,我不知道为什么。

这是工作版本:

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

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    相关资源
    最近更新 更多