关于 grails “createCriteria” 的新手问题

发布于 2024-11-13 07:05:12 字数 668 浏览 3 评论 0原文

我是 Grails 标准构建器的新手,有人可以解释一下以下内容是什么意思吗?

def c = Account.createCriteria()
         def results = c {

             like("holderFirstName", "Fred%")
             and {
                 between("balance", 500, 1000)
                 eq("branch", "London")
             }
             maxResults(10)
             order("holderLastName", "desc")
         }

这是否意味着

  • Select * from account where holderFirstName 如“fred%”和 (500 和 1000 ** 之间的余额 ** 和 ** branch='london')
  • 从帐户中选择 *,其中 holderFirstName 如“fred%”和 (500 和 1000 之间的余额 **或** Branch='london')

如果我想同时使用“or”和“and”,我该怎么做?

I am new to Grails criteria builder, can someone please explain what does the following mean?

def c = Account.createCriteria()
         def results = c {

             like("holderFirstName", "Fred%")
             and {
                 between("balance", 500, 1000)
                 eq("branch", "London")
             }
             maxResults(10)
             order("holderLastName", "desc")
         }

Does it mean

  • Select * from account where
    holderFirstName like 'fred%' and
    (balance between 500 and 1000 **and**
    branch='london
    ')
  • Select * from account where
    holderFirstName like 'fred%' and
    (balance between 500 and 1000 **or**
    branch='london
    ')

If i want to use "or" and "and" together how do I do that?

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

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

发布评论

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

评论(2

归途 2024-11-20 07:05:12

您的示例将执行为:

select * from account 
where holderFirstName like 'Fred%' 
and balance between 500 and 1000 
and branch = 'London'

所有顶级条件都隐含为“与”在一起。您可以创建相同的条件:

def c = Account.createCriteria()
def results = c {
    like("holderFirstName", "Fred%")
    between("balance", 500, 1000)
    eq("branch", "London")
    maxResults(10)
    order("holderLastName", "desc")
}

要获得第二个查询,请使用此条件:

def c = Account.createCriteria()
def results = c {
    like("holderFirstName", "Fred%")
    or {
        between("balance", 500, 1000)
        eq("branch", London")
    }
    maxResults(10)
    order("holderLastName", "desc")
}

嵌套您的 and / or 闭包以获得更复杂的条件。

Your example would execute as:

select * from account 
where holderFirstName like 'Fred%' 
and balance between 500 and 1000 
and branch = 'London'

All top level conditions are implied to be AND'ed together. You could create the same Criteria as:

def c = Account.createCriteria()
def results = c {
    like("holderFirstName", "Fred%")
    between("balance", 500, 1000)
    eq("branch", "London")
    maxResults(10)
    order("holderLastName", "desc")
}

To get your second query use this Criteria:

def c = Account.createCriteria()
def results = c {
    like("holderFirstName", "Fred%")
    or {
        between("balance", 500, 1000)
        eq("branch", London")
    }
    maxResults(10)
    order("holderLastName", "desc")
}

Nest your and / or closures for more complex Criteria.

怪异←思 2024-11-20 07:05:12

你现在的条件就是平衡和分支这两个条件中的“和”。因此,

从帐户中选择*,其中holderFirstName如'fred%'和(500和1000之间的余额分支='london')是正确的,只是它将最多保存10个结果,并将按以“holderLastName”为降序排列的基础。

要一起使用andor,您还需要在条件中指定or块,因此您的条件将类似于

Account.createCriteria()
         def results = c {

             like("holderFirstName", "Fred%")
             and {
                 between("balance", 500, 1000)
                 eq("branch", "London")
             }
             or{
                 eq("prop1", prop1)
                 eq("prop2",prop2)
             }
             maxResults(10)
             order("holderLastName", "desc")
         }

此条件平衡条件和分支条件之间的。还有 prop1 和 prop2 之间的

Your current criteria means "and" in the two conditions of balance and branch. so

Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 and branch='london') is correct, just that it will hold maximum 10 results and will be sorted on the basis of "holderLastName" in descending order.

To use and and or together you also need to specify an or block in criteria, so your criteria will look something like

Account.createCriteria()
         def results = c {

             like("holderFirstName", "Fred%")
             and {
                 between("balance", 500, 1000)
                 eq("branch", "London")
             }
             or{
                 eq("prop1", prop1)
                 eq("prop2",prop2)
             }
             maxResults(10)
             order("holderLastName", "desc")
         }

in this criteria there is an and between balance and branch conditions. and also an or between prop1 and prop2

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