Mysql::Error: 子查询返回多于 1 行: [英] Mysql::Error: Subquery returns more than 1 row:

查看:77
本文介绍了Mysql::Error: 子查询返回多于 1 行:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 rails 应用程序中,我使用 find_by_sql() 运行 sql 查询,因为我需要子查询.如果我执行第一个或第二个查询,这会起作用,但是当我将它们与 AND 一起添加时,它开始抱怨子查询中超过 1 行.

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.

我希望返回符合条件的所有行(记录).这里需要修复/更改什么?什么告诉 mysql 我只想要 1 行?

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?

这是在 rails 日志中查看的结果 SQL:

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)

推荐答案

这一行:

AND (select 0 from status_changes sc

该不该

AND exists (select 0 from status_changes sc

这篇关于Mysql::Error: 子查询返回多于 1 行:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆