【发布时间】: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的效率也稍高一些。 -
嗯...不确定在代码中排序是否更有效,但感谢您提供有关使用“介于”作为日期范围的提示。