【问题标题】:LIKE and CONCAT in QueryDslQueryDsl 中的 LIKE 和 CONCAT
【发布时间】:2016-04-28 01:27:41
【问题描述】:

如何在queryDsl中编写这个查询

SELECT a.id, 
(SELECT count(*) FROM ancestors_table t where t.ancestors LIKE CONCAT('%,',a.id,',%') )
  FROM ancestors_table a; 

我在 LIKE CONCAT('%,',a.id,',%') 部分苦苦挣扎。

解决方案

where(t.ancestors.like(
                Expressions.stringTemplate("'%,'")
               .concat(Expressions.stringTemplate("{0}" , a.id ))
               .concat(Expressions.stringTemplate("',%'")) 
))

【问题讨论】:

    标签: querydsl


    【解决方案1】:

    不完全确定,但类似这样:

    where(t.ancestors.like(Expressions.asString("%").concat(a.id).concat("%")))
    

    如果a.id是一个数字,那么你需要把它转换成一个字符串:

    where(t.ancestors.like(Expressions.asString("%").concat(a.id.stringValue()).concat("%")))
    

    【讨论】:

    • 嗨,我收到了这个The method concat(Expression<String>) in the type StringExpression is not applicable for the arguments (NumberPath<Integer>)
    • @Youssef:好吧,concat() 对数字没有意义。 LIKE 对数字也没有意义。不在 QueryDSL 中,也不在 SQL 中。你可能需要一些演员。您不可能在ancestor 列中存储逗号分隔的值列表吗?这是一个非常糟糕的数据库设计。
    • 是的,我正在这样做 :D 但为什么这是一个糟糕的设计.. 我如何进行分层查询以返回给定 id 的所有后代或返回给定 ID 的所有祖先的查询身份证?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 2019-06-15
    • 2013-05-16
    • 2016-11-22
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多