Grails 查找具有特定分钟的条目

发布于 2024-11-05 08:40:43 字数 306 浏览 4 评论 0原文

我有几个领域类每分钟生成大量数据集。像这样的事情:

class Foo{
    String name
    Date received
    int value
}

我想完成此 MySQL 查询返回的相同结果集:

SELECT * FROM foo WHERE received BETWEEN ? AND ? AND MINUTE(received) = ?

这可以使用 NamedQuery 或 findAllBy... 吗?

谢谢。

I have several domain classes which generate large data sets each minute. Something like this:

class Foo{
    String name
    Date received
    int value
}

I want to accomplish the same result set this MySQL query returns:

SELECT * FROM foo WHERE received BETWEEN ? AND ? AND MINUTE(received) = ?

Is this possible using a NamedQuery or findAllBy... ?

Thanks.

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

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

发布评论

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

评论(3

高冷爸爸 2024-11-12 08:40:43

嗯,由于HQL确实提供了微小的功能,所以我自己找到了解决方案。由于 Foo 是许多其他类的一部分,我想要一个适用于所有类的通用方法。无论如何,这是 Foo 和朋友们的解决方案:

clazz.executeQuery(
'from ' + clazz.getSimpleName() + ' c where c.received between :from and :to 
and minute(received) = :period', [from:d1, to:d2, period:0])

无论如何,谢谢 Don。

Well, since HQL does provide a minute function, I was able to find a solution by myself. Since Foo is part of many other classes I wanted a common method for all of them. Anyway, here's the solution for Foo and friends:

clazz.executeQuery(
'from ' + clazz.getSimpleName() + ' c where c.received between :from and :to 
and minute(received) = :period', [from:d1, to:d2, period:0])

Thanks anyway Don.

情话难免假 2024-11-12 08:40:43

为了避免 HQL,您可以使用 sqlRestriction。但它不支持参数。

In order to avoid HQL, you can use sqlRestriction in a Criteria. It doesn't support parameters, though.

时光礼记 2024-11-12 08:40:43
// Some arbitrarily chosen values for the query placeholders
Date minReceived = new Date()
Date maxReceived = minReceived + 5    
Integer minute = 0

Foo.findAll( 
    "from Foo where received between :min and :max and minute(received) = :minute", 
    [min: minReceived, max: maxReceived, minute: minute])
// Some arbitrarily chosen values for the query placeholders
Date minReceived = new Date()
Date maxReceived = minReceived + 5    
Integer minute = 0

Foo.findAll( 
    "from Foo where received between :min and :max and minute(received) = :minute", 
    [min: minReceived, max: maxReceived, minute: minute])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文