0

我有一个以下查询,它根据订单优先级给出记录。我想把这个传统的 mySql 查询翻译成 rails ORM,但我做不到。
我的查询如下:

SELECT *
FROM user_questions q
WHERE question_sequence_number IN (11,13,16,19) 
OR id IN (198,199,200,201,202)
ORDER BY (question_sequence_number IN (11,13,16,19)) DESC,
      CASE WHEN question_sequence_number IN (11,13,16,19) 
           THEN question_sequence_number
           ELSE id
      END

有人可以帮我解决这个问题吗?

4

1 回答 1

0

最终我只能自己写了。上面的查询将被转换为rails中的ORM如下:

UserQuestion.where("question_sequence_number IN (?) OR id IN (?)", [11,13,16,19], [198,199,200,201,202]).order("question_sequence_number IN (#{[11,13,16,19].join(',')}) DESC, CASE WHEN question_sequence_number IN (#{[11,13,16,19].join(',')}) THEN question_sequence_number ELSE id END" )

谢谢。

于 2013-08-31T12:51:21.273 回答