gpt4 book ai didi

grails - grails标准:如何选择mysql中的所有列并同时格式化日期,然后使用like

转载 作者:行者123 更新时间:2023-12-02 15:23:22 25 4
gpt4 key购买 nike

我目前正在为我的页面创建搜索功能。数据将来自数据库。

在HTML中,日期以 dd / mm / yyyy 格式显示。我想做的是,在选择查询中,我想选择所有列,并且日期应使用该格式显示,并且我有几个类似的条件将用于检查是否匹配。

如何使用条件中的一条语句选择所有列,日期格式并同时检查所有匹配项?

到目前为止,这是我所做的,但这是错误的。

 searchString = searchString.toLowerCase() 

def employeeSearchCri = Employee.createCriteria();
def searchedEmployeeList = employeeSearchCri.list(){
or {
ilike("employeeNo", "%" + searchString + "%")
ilike("firstName", "%" + searchString + "%")
ilike("lastName", "%" + searchString + "%")
ilike("middleName", "%" + searchString + "%")
ilike("jobPosition", "%" + searchString + "%")
ilike("date_format(hireDate, '%d/%m/%Y' )", "%" + searchString + "%")
ilike("status", "%" + searchString + "%")
}
and{
eq("companyId",companyId)
}
}
return searchedEmployeeList;

最佳答案

我建议的第一件事是注释掉所有喜欢的人,除了一个。选择任何一个。重点是只让其中之一工作。然后,直到那时,再添加一个ilike。

其次,确认您使用的模式(例如%SEARCHSTRING%)对您使用的数据库的LIKE运算符有效。

第三,不幸的是,一旦它开始工作,它将无法正常工作。例如,多字搜索不适用于您的方法。搜索很难。我的工作效果很好,可是男孩真是痛苦。如果您希望它做对,最好让专业人士来处理它,例如使用Apache Lucene

匹配日期

就是说,处理日期的最佳方法是将其转换为Date对象并进行匹配。您可以使用如下形式:

import java.text.SimpleDateFormat

def searchDate

try {
searchDate = new SimpleDateFormat('dd/MM/yyyy').parse(searchString)
} catch (java.text.ParseException e) { }

...

or {
...
if(searchDate) eq("hireDate", searchDate)
}

小费

您可以使用 GString,以更简洁的形式重写自己的喜欢:
ilike("employeeNo", "%$searchString%")

并且,如果您要构建要使用ilike的列的列表,则可以简化查询。这是一个完整的示例:
searchString = searchString.toLowerCase()

def columns = [
"employeeNo",
"firstName",
"lastName",
"middleName",
"jobPosition",
"status"
]

def searchDate

try {
searchDate = new java.text.SimpleDateFormat('dd/MM/yyyy').parse(searchString)
} catch (java.text.ParseException e) { }

def employeeSearchCri = Employee.createCriteria();
def searchedEmployeeList = employeeSearchCri.list() {
or {
columns.each { ilike(it, "%$searchString%") }
if(searchDate) eq("hireDate", searchDate)
}

and{
eq("companyId",companyId)
}
}

return searchedEmployeeList

关于grails - grails标准:如何选择mysql中的所有列并同时格式化日期,然后使用like,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32062449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com