在Sequelize中使用MySQL变量

时间:2016-06-08 19:35:49

标签: mysql node.js orm sequelize.js

我试图为每个不同的值抓取最多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 个答案:

答案 0 :(得分: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;