要么我今天没有专注,要么Slick的入门文档设计得不好。 所以在Queries / Unions部分中他们有这个片段:
val q1 = coffees.filter(_.price < 8.0)
val q2 = coffees.filter(_.price > 9.0)
val unionQuery = q1 union q2
// compiles to SQL (simplified):
// select x8."COF_NAME", x8."SUP_ID", x8."PRICE", x8."SALES", x8."TOTAL"
// from "COFFEES" x8
// where x8."PRICE" < 8.0
// union select x9."COF_NAME", x9."SUP_ID", x9."PRICE", x9."SALES", x9."TOTAL"
// from "COFFEES" x9
// where x9."PRICE" > 9.0
val unionAllQuery = q1 ++ q2
// compiles to SQL (simplified):
// select x8."COF_NAME", x8."SUP_ID", x8."PRICE", x8."SALES", x8."TOTAL"
// from "COFFEES" x8
// where x8."PRICE" < 8.0
// union all select x9."COF_NAME", x9."SUP_ID", x9."PRICE", x9."SALES", x9."TOTAL"
// from "COFFEES" x9
// where x9."PRICE" > 9.0
然后他们说:
Unlike union which filters out duplicate values, ++ simply concatenates the results of the individual queries, which is usually more efficient.
我认为q1
和q2
没有产生重复。因此,它们提供的错误查询示例用于说明union
和++
之间的真正差异,或者我没有获得重要的内容。你能帮忙吗?
答案 0 :(得分:1)
在这种特定情况下,没有重复项,因为q1
和q2
之间没有交集。也许将查询更改为
val q1 = coffees.filter(_.price < 8.0)
val q2 = coffees.filter(_.price < 9.0)
会是一个更好的例子。无论如何,的底线是:
q1 union q2
转换为SQL UNION
q1 ++ q2
转换为SQL UNION ALL