【问题标题】:IF funtion in QueryDslQueryDsl 中的 IF 函数
【发布时间】:2021-06-05 22:56:54
【问题描述】:

如何在queryDsl中编写这个查询

select * from Table order by if(a = 0, b, a) desc

我在 if(a = 0, b, a) 部分苦苦挣扎

解决方案

orderBy(Expressions.stringTemplate("if({0} = 0, {1}, {2})", a, b, a).desc())

【问题讨论】:

  • 我解决了 orderBy((new CaseBuilder().when(a.eq(0)).then(b).otherwise(a)).desc())

标签: querydsl


【解决方案1】:

orderBy((new CaseBuilder().when(a.eq(0)).then(b).otherwise(a)).desc()) 是执行此操作的首选方式。模板表达式也可以工作,但更容易受到查询注入错误的影响。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 2015-08-28
    • 1970-01-01
    • 2018-06-25
    • 2017-04-16
    • 2021-07-31
    • 2022-07-13
    • 2018-11-13
    相关资源
    最近更新 更多