如何使用 Exposed 在两个字段上连接两个表?
How to join two tables on two fields using Exposed?
我有以下数据库表:
// CurrenciesTable
object CurrenciesTable : Table("currencies") {
val symbol = varchar("symbol", 48)
val name = varchar("name", 48)
override val primaryKey = PrimaryKey(symbol)
}
// OrdersTable
object OrdersTable : IntIdTable("orders") {
val baseCurrency = varchar("base_currency", 48)
val counterCurrency = varchar("counter_currency", 48)
val price = decimal("price", DECIMAL_PRECISION, DECIMAL_SCALE)
val createdAtEpochSecond = long("created_at_epoch_second")
}
在 OrdersTable 中,我必须要引用 CurrenciesTable 的字段:
baseCurrency
counterCurrency
我想 select 来自 OrdersTable 的记录,并在两个字段上将它们与 CurrenciesTable 连接起来。所以我得到了每种货币的符号和名称。
这是我的 DSL 查询,仅加入 baseCurrency 字段。
// Exposed DSL
OrdersTable.join(CurrenciesTable, JoinType.INNER, OrdersTable.baseCurrency, CurrenciesTable.symbol)
.selectAll()
.forEach {
// Getting OrdersTable record data
it[OrdersTable.id].value
it[OrdersTable.price]
it[OrdersTable.createdAtEpochSecond]
// Getting CurrenciesTable record data (for baseCurrency only)
it[CurrenciesTable.symbol]
it[CurrenciesTable.name]
}
我尝试按如下方式进行第二次连接:
// Exposed DSL
OrdersTable.join(CurrenciesTable, JoinType.INNER, OrdersTable.baseCurrency, CurrenciesTable.symbol)
.join(CurrenciesTable, JoinType.INNER, OrdersTable.counterCurrency, CurrenciesTable.symbol)
但是,我得到以下异常。
Caused by: java.sql.SQLSyntaxErrorException: Not unique table/alias: 'currencies'
尝试为 CurrenciesTable
添加别名:
OrdersTable.innerJoin(CurrenciesTable.alias("baseCurrency"), { OrdersTable.baseCurrency }, { CurrenciesTable.symbol })
.innerJoin(CurrenciesTable, { OrdersTable.counterCurrency }, { CurrenciesTable.symbol })
我有以下数据库表:
// CurrenciesTable
object CurrenciesTable : Table("currencies") {
val symbol = varchar("symbol", 48)
val name = varchar("name", 48)
override val primaryKey = PrimaryKey(symbol)
}
// OrdersTable
object OrdersTable : IntIdTable("orders") {
val baseCurrency = varchar("base_currency", 48)
val counterCurrency = varchar("counter_currency", 48)
val price = decimal("price", DECIMAL_PRECISION, DECIMAL_SCALE)
val createdAtEpochSecond = long("created_at_epoch_second")
}
在 OrdersTable 中,我必须要引用 CurrenciesTable 的字段:
baseCurrency
counterCurrency
我想 select 来自 OrdersTable 的记录,并在两个字段上将它们与 CurrenciesTable 连接起来。所以我得到了每种货币的符号和名称。
这是我的 DSL 查询,仅加入 baseCurrency 字段。
// Exposed DSL
OrdersTable.join(CurrenciesTable, JoinType.INNER, OrdersTable.baseCurrency, CurrenciesTable.symbol)
.selectAll()
.forEach {
// Getting OrdersTable record data
it[OrdersTable.id].value
it[OrdersTable.price]
it[OrdersTable.createdAtEpochSecond]
// Getting CurrenciesTable record data (for baseCurrency only)
it[CurrenciesTable.symbol]
it[CurrenciesTable.name]
}
我尝试按如下方式进行第二次连接:
// Exposed DSL
OrdersTable.join(CurrenciesTable, JoinType.INNER, OrdersTable.baseCurrency, CurrenciesTable.symbol)
.join(CurrenciesTable, JoinType.INNER, OrdersTable.counterCurrency, CurrenciesTable.symbol)
但是,我得到以下异常。
Caused by: java.sql.SQLSyntaxErrorException: Not unique table/alias: 'currencies'
尝试为 CurrenciesTable
添加别名:
OrdersTable.innerJoin(CurrenciesTable.alias("baseCurrency"), { OrdersTable.baseCurrency }, { CurrenciesTable.symbol })
.innerJoin(CurrenciesTable, { OrdersTable.counterCurrency }, { CurrenciesTable.symbol })