gpt4 book ai didi

mysql - 执行 where 条件时未知列 Sequelize 子查询字段

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:46 24 4
gpt4 key购买 nike

我试图创建一个查询来查找价格小于或等于 avgPrice 的产品。

下面是示例查询。

await Product.findAll({
attributes: [
'id', 'name', 'price',
[sequelize.literal(`SELECT AVG(price) from Products`), 'avgPrice']
],
where: {
price: {
[Sequelize.Op.lte]: sequelize.col('avgPrice')
}
}

但它给出了以下错误。

Unknown column 'Products.avgPrice' in 'where clause'

我还尝试复制由sequelize生成的查询并在mysql中手动执行它,但它给出了相同的错误。

这不是我们运行查询的方式吗?

编辑:我将查询要求从avgPrice < 100更改为price < avgPrice。不过,问题里的问题是一样的。

最佳答案

如果您想根据聚合列进行过滤,则需要将 WHERE 子句更改为 HAVING

关于mysql - 执行 where 条件时未知列 Sequelize 子查询字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59453957/

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