Mysql order by子句在播放2.1.2中破坏了NORM select查询
我在游戏2.1.2中有以下代码Mysql order by子句在播放2.1.2中破坏了NORM select查询,mysql,anorm,Mysql,Anorm,我在游戏2.1.2中有以下代码 def list(startDate:String, endDate:String, page:Option[Int], pageSize:Option[Int] ) = { DB.withConnection { implicit connection => val c = SQL(""" select employeeId, CertT
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
}
}
def列表(开始日期:字符串,结束日期:字符串,页面:选项[Int],页面大小:选项[Int])={
DB.withConnection{隐式连接=>
val c=SQL(“”)
选择
雇员ID,
证书类型,
ct.name,
可应用的
由于
年(到期)为年到期,月(到期)为月到期,日(到期)为日到期,
年(已完成)、月(已完成)、日(已完成),
年(上)、月(上)、日(上),
地位
从证书
在certtype=ct.code上加入certtype ct
其中到期日{startDate}和CertType不在(500600)
到期订购
"""
)
.on('index->page,'pageSize->pageSize,'startDate->startDate,'endDate->endDate)()。收集{
案例行(
雇员ID:很长,
证书类型:Int,
名称:String,
适用:Int,
一些(到期日:java.sql.Date),
有些(到期日:长),有些(到期日:长),有些(到期日:长),
一些(完成年份:长)、一些(完成月份:长)、一些(完成日期:长),
一些(最后一年:长),一些(最后一个月:长),一些(最后一天:长),
状态:长)=>
提醒(
雇员ID,
证书类型,
名称
如果(适用>0)为真,否则为假,
制作日期(到期年、到期月、到期日),
完成日期(完成年、完成月、完成日),
makeDate(最后一年、最后一个月、最后一天),
地位);
}
c、 托利斯特
}
}
唯一有效的方法是删除“order by”子句。Order by仅适用于ct.name等字符串字段。 否则,列表为空
我没有收到任何错误消息,只是一个空白列表。好的,现在可以了,我不知道为什么 以下是工作版本:
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
}
}
实际上,从效率的角度来看,代码排序通常比数据库排序更可取。此外,SQL order by中的AFAIK也适用于日期。阿诺姆的问题可能是什么?你能改用斯力克吗?或者不要在数据库中进行排序。另外,在开始日期和结束日期之间到期的
也稍微有效一些。嗯。。。不确定在代码中排序是否更有效,但感谢您提供有关在日期范围中使用“between”的提示。