Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIKE and CONCAT in QueryDsl

Tags:

querydsl

How to write this query in queryDsl

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

I struggle with the LIKE CONCAT('%,',a.id,',%') part.

THE SOLUTION

where(t.ancestors.like(
                Expressions.stringTemplate("'%,'")
               .concat(Expressions.stringTemplate("{0}" , a.id ))
               .concat(Expressions.stringTemplate("',%'")) 
))
like image 210
Hayi Avatar asked Apr 28 '16 01:04

Hayi


1 Answers

Not entirely sure, but something like this:

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

If a.id is a number then you need to convert it into a string:

where(t.ancestors.like(Expressions.asString("%").concat(a.id.stringValue()).concat("%")))
like image 86
a_horse_with_no_name Avatar answered Nov 07 '22 23:11

a_horse_with_no_name