-2

我必须将用 sql 编写的查询翻译成 ruby​​ (activeRecord / Linq),但我找不到正确的语法来做这件事,请帮忙。

我想知道在一个节日中进行这 4 项活动的唯一用户数量。sql查询如下:

`SELECT count(distinct user) FROM stats where festivalId='fest000'
and user in (select user from stats where activity='checkins')
and user in (select user from stats where activity='programs')
and user in (select user from stats where activity='status')
and user in  (select user from stats where activity='pictureUpload');`

我怎么能做到呢?我在哪里可以找到至少类似的示例或好的文档来弄清楚?是否可以使用恒定的“用户”,这样我就可以完成所有查询,然后将它们放在一起或类似的东西?

我很绝望,我找不到解决方案,我不知道该怎么办......提前谢谢!

4

1 回答 1

0

我想您需要找出具有festivalId='fest000'activity 'checkins' AND 'programs' AND 'status' AND'pictureUpload')

然后下面的查询应该工作

SELECT COUNT(*) FROM stats 
WHERE festivalId='fest000' AND activity IN ('checkins', 'programs', 'status','pictureUpload') 
GROUP BY user 
HAVING COUNT(DISTINCT activity) = 4

您也可以轻松地以 ruby​​ 方式转换此查询

Stat.where("festivalId=? AND activity IN (?)", 'fest000', ['checkins', 'programs', 'status','pictureUpload']).group('user').having("COUNT(distinct activity) = 4").count
于 2013-07-03T11:23:58.173 回答