scala 如何在 SLICK 中使用 SQL“LIKE”运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14700821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:55:30  来源:igfitidea点击:

How to use SQL "LIKE" operator in SLICK

scalascalaqueryslick

提问by wassertim

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?

也许是一个愚蠢的问题。但到目前为止我还没有找到答案。那么如何在SLICK 中表示 SQL 的“LIKE”操作符呢?

回答by Faiz

Exactly as you normally would!

就像你平时一样!

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

Will generate SQL like

会生成 SQL 之类的

SELECT name, price FROM coffees WHERE NAME like '%expresso%';

回答by Abhishek Jangalwa

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)

然后你可以使用你的数据库运行它: val res = db.run(query.result)

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

当然,res这里有一个未来,您需要使用 await 来获得实际结果。