搜索具有逗号分隔值且包含给定列表中的任何元素的记录

发布于 2024-10-31 10:47:12 字数 427 浏览 7 评论 0原文

我有一个域类 Schedule,其属性“days”包含逗号分隔值,例如“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()

我需要从给定列表中获取任意一天的时间表列表,例如 [2,8,11]。

输出:[schedule1]

如何编写相同的条件查询或 HQL。我们可以添加前缀 &如果有帮助的话,请在日期后加上逗号,例如“,2,5,6,8,9”。

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

近箐 2024-11-07 10:47:12

希望您对这种非规范化有充分的理由 - 否则最好将列表保存到子表中。

不然查询起来会很复杂。喜欢:

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

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"
      }
    }
  }
请别遗忘我 2024-11-07 10:47:12

在 MySQL 中,有一个 SET 数据类型和 FIND_IN_SET 函数,但我从未在 Grails 中使用过它。某些数据库支持标准 SQL2003 ARRAY 数据类型,用于在字段中存储数组。可以使用 hibernate 用户类型(Grails 支持)来映射它们。

如果您使用 MySQL,FIND_IN_SET 查询应与 Criteria API sqlRestriction 配合使用:
http://grails.org/doc /latest/api/grails/orm/HibernateCriteriaBuilder.html#sqlRestriction(java.lang.String)
如果您关心性能并且确实需要进行非规范化,那么使用 SET+FIND_IN_SET 会使查询比类似查询更高效。

In MySQL there is a SET datatype and FIND_IN_SET function, but I've never used that with Grails. Some databases have support for standard SQL2003 ARRAY datatype for storing arrays in a field. It's possible to map them using hibernate usertypes (which are supported in Grails).

If you are using MySQL, FIND_IN_SET query should work with the Criteria API sqlRestriction:
http://grails.org/doc/latest/api/grails/orm/HibernateCriteriaBuilder.html#sqlRestriction(java.lang.String)
Using SET+FIND_IN_SET makes the queries a bit more efficient than like queries if you care about performance and have a real requirement to do denormalization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文