尝试使用ActiveRecord获取此查询,我该如何正确编写?
SELECT AVG(IF(condition)) AS average_id
FROM (SELECT * FROM `table`
WHERE `id` = 1
ORDER BY `date` DESC
LIMIT 10) `table`;
答案 0 :(得分:0)
鉴于table
由名为MyModel
的模型建模 - 上述内容将大致翻译为:
MyModel.average('IF(condition)').where(id: 1).order(date: :desc).limit(10)