将 MySQL 变量与 Sequelize 一起使用
Posted
技术标签:
【中文标题】将 MySQL 变量与 Sequelize 一起使用【英文标题】:Use MySQL variables with Sequelize 【发布时间】:2016-06-08 19:35:48 【问题描述】:我正在尝试为每个不同的值获取最多 4 行。这个查询我有工作。
SET @currcount = NULL, @currvalue = NULL;
SELECT * FROM (
SELECT
*,
@currcount := IF(@currvalue = quantity, @currcount + 1, 1) AS rank,
@currvalue := quantity AS whatever
FROM wines
where restaurant_id = 16 and owner_id is null
ORDER BY quantity DESC
) AS x WHERE rank <= 4
但现在我无法让它与 Sequelize 一起使用。我可以在 Sequelize 中使用 @variable 吗?
有人做过吗?还是我只需要使用原始查询?
谢谢!
【问题讨论】:
【参考方案1】:我不确定,但是像这样将您的 2 个查询放在一起 并尝试:sequelize.query(.....
SELECT * FROM (
SELECT
*,
@currcount := IF(@currvalue = quantity, @currcount + 1, 1) AS rank,
@currvalue := quantity AS whatever
FROM wines
where restaurant_id = 16 and owner_id is null
ORDER BY quantity DESC
) AS x
CROSS JOIN ( SELECT @currcount := NULL, @currvalue := NULL) as parameter
WHERE rank <= 4;
【讨论】:
以上是关于将 MySQL 变量与 Sequelize 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Sequelize 中使用 MySQL JSON 数据类型