gpt4 book ai didi

使用嵌套选择进行 Sequelize 的 SQL

转载 作者:行者123 更新时间:2023-12-03 22:30:33 28 4
gpt4 key购买 nike

需要在Sequelize Node js中编写如下SQL Query

select a, b, c
from (
select tbl.*,
count(*) over(partition by a) cnt,
row_number() over (partition by a order by b) brn,
row_number() over (partition by a order by c) crn
from tbl
where c in (4, 5)
) t
where cnt = 2 and brn = crn;
这是我想出的。我不知道把条件 where cnt = 2 and brn = crn; 放在哪里
t.findAll({
attributes: {
include: [
[sequelize.literal('count(*) over(partition by a)'),'cnt'],
[sequelize.literal('row_number() over (partition by a order by b)'),'brn'],
[sequelize.literal('row_number() over (partition by a order by c)'),'crn'],
]
},
where: {
c: {[Op.in]:[4,5]},
}
})

最佳答案

Sequelize 似乎不支持从子查询( https://github.com/sequelize/sequelize/issues/5354 )中进行选择
您可以改用原始查询( https://sequelize.org/master/manual/raw-queries.html ):

const { QueryTypes } = require('sequelize');
let result = await sequelize.query(`
select a, b, c
from (
select tbl.*,
count(*) over(partition by a) cnt,
row_number() over (partition by a order by b) brn,
row_number() over (partition by a order by c) crn
from tbl
where c in (4, 5)
) t
where cnt = 2 and brn = crn;
`, {type: QueryTypes.SELECT});

关于使用嵌套选择进行 Sequelize 的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67448422/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com