使用查找的 Rails SQL 查询 [英] Rails SQL Query with find
本文介绍了使用查找的 Rails SQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在 rails 控制器中使用 find 编写这个 SQL 查询:
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)
我将如何做到这一点?我正在使用 Rails 框架和 MySQL.提前致谢.
How will I do this? I am using Rails framework and MySQL. Thanks in advance.
推荐答案
简单方法:
ids = LevelsQuestion.all(:select => "question_id",
:conditions => "level_id = 15").collect(&:question_id)
Question.all(:select => "id, name", :conditions => ["id not in (?)", ids])
一枪:
Question.all(:select => "id, name",
:conditions => ["id not in (select question_id from levels_questions where level_id=15)"])
这篇关于使用查找的 Rails SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文