sequelize - 在使用内连接时为 SELECT 查询指定属性
sequelize - Specifying attributes for SELECT queries while using inner join
我想从主 table 帐户中获取数据并将其与帐户历史记录进行比较。
两者之间存在On To many关系table
SELECT account."userName", (account."postNumber" - accountHistory."postNumber") AS postNumber
FROM public."Accounts" AS account INNER JOIN public."AccountHistories" AS accountHistory
ON account."userName" = accountHistory."userName"
WHERE accountHistory."scrappingDate" = '2022-01-08 23:59:39+01'
ORDER BY postNumber DESC;
我的问题是如何使用 sequilize 进行查询?
我试过了,但我无法更改属性的名称并进行减法
await this.accountRepository.findAll({
where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'},
include: [{
model: AccountHistory,
required: false
}],
attributes: ['userName',['$accountHistory.postNumber $', 'postNumber '],postNumber ],
order: [[$accountHistory.createdAt$', 'DESC']],
})
您可能需要使用 Sequelize.literal 作为别名属性执行计算,例如:
const { literal } = require('sequelize')
await this.accountRepository.findAll({
where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'},
include: [{
model: AccountHistory,
required: false
}],
attributes: [
'userName',
[
literal('"account"."postNumber" - "accountHistory"."postNumber"'),
'postNumber'
]
],
order: [[$accountHistory.createdAt$', 'DESC']],
})
根据您的 SQL 方言,确切的语法可能不起作用 - 我不熟悉您使用的 $
符号
我想从主 table 帐户中获取数据并将其与帐户历史记录进行比较。 两者之间存在On To many关系table
SELECT account."userName", (account."postNumber" - accountHistory."postNumber") AS postNumber
FROM public."Accounts" AS account INNER JOIN public."AccountHistories" AS accountHistory
ON account."userName" = accountHistory."userName"
WHERE accountHistory."scrappingDate" = '2022-01-08 23:59:39+01'
ORDER BY postNumber DESC;
我的问题是如何使用 sequilize 进行查询? 我试过了,但我无法更改属性的名称并进行减法
await this.accountRepository.findAll({
where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'},
include: [{
model: AccountHistory,
required: false
}],
attributes: ['userName',['$accountHistory.postNumber $', 'postNumber '],postNumber ],
order: [[$accountHistory.createdAt$', 'DESC']],
})
您可能需要使用 Sequelize.literal 作为别名属性执行计算,例如:
const { literal } = require('sequelize')
await this.accountRepository.findAll({
where: {'$accountHistory.createdAt$' : '2022-01-08 23:59:39+01'},
include: [{
model: AccountHistory,
required: false
}],
attributes: [
'userName',
[
literal('"account"."postNumber" - "accountHistory"."postNumber"'),
'postNumber'
]
],
order: [[$accountHistory.createdAt$', 'DESC']],
})
根据您的 SQL 方言,确切的语法可能不起作用 - 我不熟悉您使用的 $
符号