我有以下表格定义(简化):
class Houses(tag: Tag) extends Table[HouseRow](tag, "HOUSE") {
def houseId = column[Long]("HOUSE_ID", O.NotNull, O.PrimaryKey, O.AutoInc)
def houseName = column[String]("HOUSE_NAME", O.NotNull)
def houseType = column[String]("HOUSE_TYPE", O.NotNull)
def uniqueHouseName = index("UQ_HOUSE_NAME_HOUSE_TYPE", (houseName, houseType), true)
def * = (houseId, houseName, houseType) <> (HouseRow.tupled, HouseRow.unapply)
}
val houses = TableQuery[Houses]
我想选择匹配一组uniqueHouseName索引的房屋,如下所示。
case class HouseKey(houseName: String, houseType: String)
val houseKeys: Seq(HouseKey("name1", "type1"), HouseKey("name2", "type2"))
天真inSetBind
过滤器将匹配例如。 HouseRow(ID, "name1", "type2")
这是不正确的。
在MySql中,我会做类似的事情:
SELECT * FROM HOUSE h
WHERE(h.HOUSE_TYPE, d.HOUSE_NAME) IN
(
SELECT 'type1' as HOUSE_TYPE, 'name1' as HOUSE_NAME
UNION
SELECT 'type2', 'name2'
);
答案 0 :(得分:2)
调整tuxdna的答案以允许任意seqs。但是,此查询目前无法预编译为SQL,并且具有运行时开销。
val filteredHouses =
houses.filter(h =>
houseKeys.map(hk => h.houseName === hk.houseName && h.houseType === hk.houseType)
.reduce(_ || _)
)
答案 1 :(得分:2)
喜欢@cvogt版本,但不会在空列表中爆炸:
down
在光滑的3.1.0中测试
答案 2 :(得分:0)
这不是完整的答案,但只有两对值,你可以这样做:
val filteredHouses = for {
h <- houses
if (h.houseName === "name1" && h.houseType === "type1") || (
h.houseName === "name2" && h.houseType === "type2")
} yield h