Mysql::错误:子查询返回超过 1 行:
在我的 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)
In my rails app, I am running a sql query using find_by_sql() since I need subqueries.
This works if I do either the first or second query but when I add them together with the AND, it starts complaining about more than 1 row in subquery.
I want all rows (records) returned that match the criteria. What needs to be fixed/changes here? What is telling mysql I only want 1 row?
Here is the resultant SQL as viewed in the rails log:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一行:
不应该吗
This line:
Shouldn't it be
据我所知,任何 SQL 服务器都不支持返回超过 1 行的子查询。
Subqueries that return more than 1 row are not supported by any SQL server, as far as I am aware.