【问题标题】:Pattern matching in squeryl queriessqueryl 查询中的模式匹配
【发布时间】:2016-09-21 23:52:47
【问题描述】:

我刚开始使用 squeryl,不知道如何编写这样的查询

SELECT ref label 
FROM x_table 
WHERE ref like x% or lable like x%

其中x 是来自用户的一些价值,尤其是我在 squeryl 中没有找到类似于 % 的符号,或者我该如何使用它。

我的版本:

val products = from(AppDB.productTable) (
    s => where ((s.label like value) or (s.ref like value))  select(s)
)

无法正常工作。

val value : Option[String]我从用户那里得到。

【问题讨论】:

  • 您是在问如何在字符串末尾附加% 字符?

标签: scala orm squeryl


【解决方案1】:

您可以尝试选择您的字段,如下所示:

val products = from(AppDB.productTable) (s => 
  where ((Some(s.label) like value) or (Some(s.ref) like value)) 
  select(s))

这将编译,因为查询会将 Option[String] 与 Option[String] 进行比较。 Squeryl 将在内部处理选项状态。

如果您只是想将通配符添加到您要比较的内容中,假设 like 子句的两边都是 Option[String] 类型,那么您可以这样做:

s.label like value.map(_ + "%")

【讨论】:

  • 不,在这种情况下我们得到一个错误 like is not a member of Some[Option[String]]...
  • s.refOption[String]valueOption[String]?您最初收到的错误是什么?
  • 是的,我正在寻找如何添加通配符,所以没关系,谢谢,但它只适用于 Option[String] 而不适用于 字符串 例如对吗?
  • 至于答案顶部建议的代码@jcern,它给出了一个错误“like is not a member of Some[Option[String]]”跨度>
  • 如果 value 是一个字符串,你可以简单地把右边 (value + "%").
【解决方案2】:

您可以使用较短的版本:

AppDB.productTable.where ((s.label like value) or (s.ref like value))

选项字段用于可空列

【讨论】:

  • 不,如果您需要获取 labelref 包含 的所有行,这样它就没有必要了值你必须使用通配符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
相关资源
最近更新 更多