0
내 질문이 매우 진부하게 들릴지 모르지만 나는 아직 해결하지 못했습니다.scala slick or query
select * from products where(category_id = 1 or category_id = 2 or category_id = 3) and (price between min and max)
내 질문이 매우 진부하게 들릴지 모르지만 나는 아직 해결하지 못했습니다.scala slick or query
select * from products where(category_id = 1 or category_id = 2 or category_id = 3) and (price between min and max)
이런 식으로 뭔가를 시도 :
가 나는 제품 표가 나는 SQL 쿼리에 쿼리 상당을 구현할 수있는 방법
class ProductsTable(tag: Tag) extends Table[Product](tag, "PRODUCTS") {
def id = column[Int]("PRODUCT_ID", O.PrimaryKey, O.AutoInc)
def title = column[String]("NAME")
def description = column[String]("DESCRIPTION")
def style = column[String]("STYLE")
def price = column[Int]("PRICE")
def category_id = column[Int]("CATEGORY_ID")
def size_id = column[Int]("SIZE_ID")
def brand_id = column[Int]("BRAND_ID")
def * = (id.?, title, description, style, price, category_id, size_id, brand_id) <>(Product.tupled, Product.unapply _)
}
및
val Products = TableQuery[ProductsTable]
에서의 표현과 같이 구현이 :
val query = Products filter { p => (p.category_id inSet List(1,2,3)) && p.price > min && p.price < max }
val result = db.run(query.result)
println(query.result.statements)
을 사용하면 어떤 검색어가 표시되는지 확인할 수 있습니다.
편집 : 추가 질문에 대한
대답. ...
val q1 = getProductsQuery() // without min or max
val q2 = getProductsQuery(maybeMin = Option(3)) // only min
val q3 = getProductsQuery(maybeMax = Option(10)) // only max
val q4 = getProductsQuery(maybeMin = Option(3), maybeMax = Option(10)) // both
하고 필요에 따라 다음 중 하나를 실행합니다
def getProductsQuery(maybeMin: Option[Int] = None, maybeMax: Option[Int] = None) = {
val initialQuery = val query = Products filter { p => (p.category_id inSet List(1,2,3)) }
val queryWithMin = maybeMin match {
case Some(min) => initialQuery filter { _.price > min }
case None => initialQuery
}
val queryWithMax = maybeMax match {
case Some(max) => queryWithMin filter { _.price < max }
case None => queryWithMin
}
queryWithMax
}
그리고 당신이 중 하나를 수행 할 수 있습니다 : 당신은 선택 사항 최소 및 최대 값을 허용 조회하는 기능을 할 수 있습니다
고마워요........... 다른 문제가 있습니다. 같은 것을 할 수있는 방법이 있습니다. 옵션 값은 최소값과 최대 값이 같습니다. –
[여기] (http : //slick.lightbend.com/doc/3.1.1/queries.html) 및 [내 블로그 게시물] (http://olivebh.com/scala-play-slick.html). –
물론 매개 변수로 ID 목록을 넣을 수도 있습니다. :) –