利用find来引导SQL查询 [英] Rails SQL Query with find

查看:219
本文介绍了利用find来引导SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  select id,name from questions 
where id not in(从level_questions where level_id = 15中选择question_id)

我该怎么做?我正在使用Rails框架和MySQL。
在此先感谢。

解决方案

简单的方法:

<$ collect(&:question_id)$ b(pre:<= c $ c> ids = LevelsQuestion.all(:select =>question_id,
:conditions =>level_id = 15 $ b Question.all(:select =>id,name,:conditions => [id not in(?),ids])



  Question.all(:select => id,name,
:conditions => [id not in(from level_questions where level_id = 15 select question_id)])


I want this SQL query to be written in rails controller using find:

select id,name from questions
where id not in (select question_id from levels_questions where level_id=15)

How will I do this? I am using Rails framework and MySQL. Thanks in advance.

解决方案

Simple way:

ids = LevelsQuestion.all(:select => "question_id", 
        :conditions => "level_id = 15").collect(&:question_id)
Question.all(:select => "id, name", :conditions => ["id not in (?)", ids])

One shot:

Question.all(:select => "id, name",
:conditions => ["id not in (select question_id from levels_questions where level_id=15)"])

这篇关于利用find来引导SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆