Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQL "LIKE" operator in SLICK

Maybe a silly question. But I have not found an answer so far. So how do you represent the SQL's "LIKE" operator in SLICK?

like image 442
wassertim Avatar asked Feb 05 '13 05:02

wassertim


2 Answers

Exactly as you normally would!

val query = for {
  coffee <- Coffees if coffee.name like "%expresso%"
} yield (coffee.name, coffee.price)

Will generate SQL like

SELECT name, price FROM coffees WHERE NAME like '%expresso%';
like image 67
Faiz Avatar answered Oct 20 '22 20:10

Faiz


This is how I got it to work:

// argMap is map of type [Str, Str]
val query = for {
    coffee <- coffees if (
      argMap.map{ case (k,v) =>
        metric.column[String](k) like s"%${v}%"
      }.reduce(_ && _)
    )
  } yield(coffee.name)

And then you can run this using your db: val res = db.run(query.result)

Of course res is a future here that you need to use await to get the actual result.

like image 3
Abhishek Jangalwa Avatar answered Oct 20 '22 20:10

Abhishek Jangalwa