[英]Grails createCriteria mysql IF statement
I'm a newbie in Grails, and I have a big problem.我是 Grails 的新手,我有一个大问题。 I have this domain:
我有这个域:
class Product{
String name
Integer priority
Double quantity
}
I want to sort Products by priority (which IS 0 or 1) - desc, and then sort by quantity.我想按优先级排序的产品(即0或1) -按数量递减,然后排序。 In mysql I do it like this:
在 mysql 我这样做:
select name, IF(quantity>=1,1,0) as q from product
where ...
order by priority desc, q desc, id desc
There is alse CASE statement in mysql, but in Grails I don't have an idea how to create criteria for this. mysql 中还有 CASE 语句,但在 Grails 中,我不知道如何为此创建条件。
Thanks in advance!提前致谢!
Product.createCriteria().list(){
projection{ // 1. because you only want to getname list.
property("name")
}
ge("quantity", 1) // 2. greater than or equal 1
order("priority", "desc" ) // by default "asc"
order("quantity", "desc" )
order("id", "desc" )
};
Only Returns the given property in the returned results when you wrapping by "projections".当您通过“投影”包装时,仅返回返回结果中的给定属性。
projections { property("firstName") }
Where a property is greater than or equal to a particular.属性大于或等于特定属性。
ge("balance", 1000)
"desc" or "asc" for descending and ascending respectively. "desc" 或 "asc" 分别用于降序和升序。 By default, results are sorted in ascending order
默认情况下,结果按升序排序
One needs to create a custom Order
(sql-generating) class to sort on arbitrary expression, or just go with SQL. 需要创建一个自定义的
Order
(SQL生成)类,以对任意表达式进行排序,或者只使用SQL。
Looks like even HQL cannot do that: Product.executeQuery('from Product p order by (id > 100)')
fails for me with HQL syntax error. 看起来甚至HQL也无法做到这一点:
Product.executeQuery('from Product p order by (id > 100)')
对我失败,出现HQL语法错误。
Try to do something around below :尝试在下面做一些事情:
select name,IF(quantity>=1,1,0) as q
from product
where ...
order by IF(priority=1,0,1), q desc, id desc
Considering Priority 1= high 0=low考虑优先级 1=高 0=低
Please do required changes请进行必要的更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.