问题描述
我有一个域类Schedule,其中包含'2,5,6,8,9'等逗号分隔值的'days'属性。
课程时间表{
字符串天数
...
}
时间表schedule1 =新时间表(天数:'2,5,6,8,9')
schedule1.save()
时间表schedule2 =新时间表(天数:'1 ,5,9,13')
schedule2.save()
我需要从给定列表中获取任何日期的时间表列表说明[2,8,11]。
输出:[schedule1]
如何编写标准查询或HQL。我们可以前缀&如果有帮助,后缀为'2,5,6,8,9'的逗号。
谢谢,
希望你有这样的非规范化的好理由 - 否则最好将列表保存到子表中。
$ b $否则,查询将变得复杂。喜欢:
def天数= [2,8,11]
//检查空天的注意事项
Schedule.withCriteria {
days.each {day - >
或{
like('username',$ day,%)//以$ day
开头('username',%,$ day,%)
like('username',%,$ day)//以$ day
}
}
}
I have a domain class Schedule with a property 'days' holding comma separated values like '2,5,6,8,9'.
Class Schedule {
String days
...
}
Schedule schedule1 = new Schedule(days :'2,5,6,8,9')
schedule1.save()
Schedule schedule2 = new Schedule(days :'1,5,9,13')
schedule2.save()
I need to get the list of the schedules having any day from the given list say [2,8,11].
Output: [schedule1]
How do I write the criteria query or HQL for the same. We can prefix & suffix the days with comma like ',2,5,6,8,9,' if that helps.
Thanks,
Hope you have a good reason for such denormalization - otherwise it would be better to save the list to a child table.
Otherwise, querying would be complicated. Like:
def days = [2,8,11]
// note to check for empty days
Schedule.withCriteria {
days.each { day ->
or {
like('username', "$day,%") // starts with "$day"
like('username', "%,$day,%")
like('username', "%,$day") // ends with "$day"
}
}
}
这篇关于搜索记录包含逗号分隔的值,其中包含给定列表中的任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!