[英]I can't figure out how to convert sql query into ruby (Nesting selects)
我必须在用Ruby-on-rails编写的网页上显示某些数据库的静态信息。 我知道我要问数据库什么,但是我不知道如何用ruby编写它。
目的是了解在同一节日中有多少用户完成了这4个活动。
我想转换为红宝石的SQL查询是:
`SELECT count(distinct usermail) FROM stats where festivalId='2013'
and usermail in (select usermail from stats where typeActivity='checkins')
and usermail in (select usermail from stats where typeActivity='programs')
and usermail in (select usermail from stats where typeActivity='status')
and usermail in (select usermail from stats where typeActivity='pictureUpload');
`
所以我的问题是我不知道如何将“选择”嵌套到大选择中。 我尝试在控制器中编写类似的东西,但没有成功。
def self.countTotalActivity(appId)
stats= Stat.where(:festivalId => appId).where(:typeActivity => ('checkins').where(:typeActivity => 'status'))
return stats.count
end
我一直在潜伏于文档中,并尝试了几天的不同尝试,但是我没有任何进展...
谢谢!!!!!!
尝试这个...
stats= Stat.select(:usermail).where(festivalId: 2013,typeActivity: ['checkins','status', 'programs', 'pictureUpload']).uniq.count
尝试跟随
Stat.where("festivalId=? AND typeActivity IN (?)", '2013', ['checkins', 'programs', 'status','pictureUpload']).group('usermail').having("COUNT(DISTINCt typeActivity) = 4").count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.