在我的 rails 应用程序中,我正在使用 find_by_sql() 运行 sql 查询,因为我需要子查询。如果我执行第一个或第二个查询,但是当我将它们与 AND 一起添加时,它会开始抱怨子查询中有超过 1 行。
我希望返回与条件匹配的所有行(记录)。这里需要修复/更改什么?什么告诉 mysql 我只想要 1 行?
这是在 rails 日志中查看的结果 SQL:
Mysql::Error: Subquery returns more than 1 row: select p.* from policies p
where exists (select 0 from status_changes sc join statuses s on sc.status_id = s.id
where sc.policy_id = p.id
and s.status_category_id = '1'
and sc.created_at between '2009-03-10' and '2009-03-12')
or exists
(select 0 from status_changes sc join statuses s on sc.status_id = s.id
where sc.created_at in
(select max(sc2.created_at)
from status_changes sc2
where sc2.policy_id = p.id
and sc2.created_at < '2009-03-10')
and s.status_category_id = '1'
and sc.policy_id = p.id)
AND (select 0 from status_changes sc
where sc.policy_id = p.id
and sc.status_id = 7
and sc.created_at between '2008-12-31' and '2009-03-12')
or exists
(select 0 from status_changes sc
where sc.created_at in
(select max(sc2.created_at)
from status_changes sc2
where sc2.policy_id = p.id
and sc2.created_at < '2008-12-31')
and sc.status_id = 7
and sc.policy_id = p.id)