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

发布于 2024-07-15 08:39:06 字数 1203 浏览 4 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-07-22 08:39:06

这一行:

AND (select 0 from status_changes sc

不应该吗

AND exists (select 0 from status_changes sc

This line:

AND (select 0 from status_changes sc

Shouldn't it be

AND exists (select 0 from status_changes sc
稚气少女 2024-07-22 08:39:06

据我所知,任何 SQL 服务器都不支持返回超过 1 行的子查询。

Subqueries that return more than 1 row are not supported by any SQL server, as far as I am aware.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文