gpt4 book ai didi

sequelize.js - 我怎么能在 sequelize 中编写这个查询?

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

考虑以下查询:SELECT Film.FilmId FROM Films WHERE NOW() BETWEEN Film.StartDate AND Film.EndDate这个查询怎么写在 sequelize 中?我知道使用 sequelize.fn('now') 我应该能够获得 NOW() mariadb 函数,但是我应该如何告诉 sequelize 使用它作为 where 子句的左操作数?

最佳答案

使用 sequelize.where() 生成一个使用 sequelize.fn('NOW') Op.between query operator 的条件,传入 Film.StartDateFilm.EndDate 的列。
有关更多信息,请参阅文档中的 Advanced queries with functions (not just columns)

// get the Op query operators
const { Op } = require("sequelize");

// ... your code

// use await to find one Film, in an async function
const film = await Film.findOne({
// just fetch the Film.FilmId
attributes: ['FilmId'],
where: {
// create where condition for NOW()
sequelize.where(sequelize.fn('NOW'), {
// BETWEEN Film.StartDate AND Film.EndDate
[Op.between]: [
sequelize.col('Film.StartDate'),
sequelize.col('Film.EndDate')
],
}),
},
});

关于sequelize.js - 我怎么能在 sequelize 中编写这个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64450588/

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