2016-06-25 24 views
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) 

答えて

0

はこのような何かを試してみてください:

私は製品表は、私は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)を使用すると、どのようなクエリが表示されるかを確認できます。

EDIT:追加の質問に対する

回答。 ...

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 
} 

そして、あなたはこれらのいずれかの操作を行うことができます:あなたは、オプションの最小値と最大値を受け入れクエリの機能を行うことができます

+0

ありがとうございます。それはうまくいきましたが、他の問題があります...同じやり方をする方法はありますが、オプションの値は最小値と最大値を合わせて –

+0

あなたは[ここで]クエリの詳細を読むことができます://slick.lightbend.com/doc/3.1.1/queries.html)と[私のブログ投稿](http://olivebh.com/scala-play-slick.html )。 –

+0

もちろん、IDリストをパラメータとして入れることもできます。:) –

関連する問題