我有以下表格定义(简化):
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'
);
最佳答案
修改tuxdna的答案以允许任意序列。但是,此查询目前无法预编译为SQL,并且具有运行时开销。
val filteredHouses =
houses.filter(h =>
houseKeys.map(hk => h.houseName === hk.houseName && h.houseType === hk.houseType)
.reduce(_ || _)
)