gpt4 book ai didi

Mysql::错误:子查询返回超过 1 行:

转载 作者:行者123 更新时间:2023-11-29 15:10:36 28 4
gpt4 key购买 nike

在我的 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)

最佳答案

这一行:

AND (select 0 from status_changes sc

不应该吗

AND exists (select 0 from status_changes sc

关于Mysql::错误:子查询返回超过 1 行:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/637613/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com