【问题标题】:Mysql::Error: Subquery returns more than 1 row:Mysql::Error:子查询返回多于 1 行:
【发布时间】:2009-03-12 06:57:03
【问题描述】:

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

【问题讨论】:

    标签: mysql sql ruby-on-rails mysql-error-1242


    【解决方案1】:

    这一行:

    AND (select 0 from status_changes sc
    

    不应该

    AND exists (select 0 from status_changes sc
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • Select id, name from user where id in (Select id From active_user) ?
      猜你喜欢
      • 1970-01-01
      • 2018-03-08
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      相关资源
      最近更新 更多