[英]sequelize - Specifying attributes for SELECT queries while using inner join
I want to fetch from the main table account and compare it with account history.我想从主表帐户中获取并将其与帐户历史记录进行比较。 there is a relationship On To many between the two table两个表之间存在 On To many 关系
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;
My question is how can I make the query with sequilize?我的问题是如何使用 sequilize 进行查询? I tried but I could not change the name of the attribute and do the subtraction我试过了,但我无法更改属性的名称并进行减法
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']],
})
You probably need to use Sequelize.literal to perform the calculation as an aliased attribute, eg:您可能需要使用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']],
})
That exact syntax might not work depending on your SQL dialect - I'm not familiar with the $
symbols you are using根据您的 SQL 方言,该确切语法可能不起作用 - 我不熟悉您使用的$
符号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.