How to use 'like' in Anorm query?

Viewed 522

My Data base is porstgres, and I am trying to write a query that will use like or ~ (postgres regex). I couldn't write this query using on.

so here is what I tried to write:

SQL(s"""SELECT id, name
       |FROM my_table 
       |WHERE name ~ '^{name}'""")
.on('name -> name).as(parser.*)

Or:

SQL(s"""SELECT id, name
       |FROM my_table 
       |WHERE name like '{name}%'""")
.on('name -> name).as(parser.*)

The code that is working for me does not use on. And it looks like this:

SQL(s"""SELECT id, name
       |FROM my_table 
       |WHERE name """.concat(s"like '$name%'").as(parser.*)

Is there a better way to achieve this?

2 Answers
Related