【发布时间】:2021-05-05 15:08:34
【问题描述】:
我有一个questions 表、一个user 表和一个answers 表,其中user_id 和question_id 作为外键。
我试过了:
select questions.title from questions
full outer join answers on answers.question_id = questions.id
where answers.user_id = 1
group by questions.id
having count(answers.id) = 0
但问题是,当 questions.id 为 null 时,它按 null 分组
理想情况下,查询应该可以使用 ActiveRecord (Ruby on Rails)。
【问题讨论】:
-
为什么不使用 ActiveRecord 而不是 SQL 来表达这一点?
-
when questions.id is null it groups by null- 为什么你的主键列可以为空? -
@tadman 我想让问题更通用,以便有更多机会得到答案,模型很明显,三个类,两个
belongs_to和两个has_many -
@max 也许我的假设是错误的,我只知道它返回了错误的结果
-
很公平,但是如果您利用模型中的关系,其中很多确实会转化为 ActiveRecord。
标签: sql ruby-on-rails