为什么MySql给出“子查询返回超过1行” 错误?
大家好,我正在从我的表格中选择我的活动记录,我的所有其他记录都很好,但是活动记录给了我错误,我的代码是
@query = Estate.find_by_sql"SELECT (e.name) as Estate_name, g.name as Governor_body,"+ “(从看台 s 中选择 count(),其中 s.estate_id = e.id AND #{filter_estates}) 作为total_stands,”+ “(从庄园 e 中选择 e.active,其中 e.active = true AND #{filter_estates})作为estate_status,”+ “(从服务 sp 中选择计数(),其中 sp.estate_id = e.id AND #{filter_estates})作为 service_providers,”+ “(从approved_vendors av中选择count(*),其中av.estate_id = e.id AND #{filter_estates})作为供应商”+ " FROM Estates e LEFT JOIN Governor_bodies g on e.governing_body_id = g.id AND #{filter_estates} "
我收到错误。
(Mysql::Error: 子查询返回超过 1 行:SELECT (e.name) as Estate_name, g.name as Governor_body,(select count() fromstand s where s.estate_id = e.id AND e.id 不为空)作为total_stands,(从 Estates e 中选择 e.active,其中 e.active = true 且 e.id 不为空)作为 Estate_status,(从服务 sp 中选择 count(),其中 sp .estate_id = e.id AND e.id IS NOT NULL) as service_providers,(从roved_vendors av中选择count(*),其中av.estate_id = e.id AND e.id IS NOT NULL)作为供应商FROM Estates e LEFT JOINgovernance_bodies g on e.governing_body_id = g.id AND e.id IS NOT NULL ):
我想显示所有处于活动状态和非活动状态的庄园。
请问各位,我该如何解决这个问题。 我正在使用Mysql数据库。
Hi guy i am selecting my active record from my table estates and all my other record are fine but the active record give me errors my code is
@query = Estate.find_by_sql"SELECT (e.name) as estate_name, g.name as governing_body,"+
"(select count() from stands s where s.estate_id = e.id AND #{filter_estates}) as total_stands, "+
"(select e.active from estates e where e.active = true AND #{filter_estates}) as estate_status, "+
"(select count() from services sp where sp.estate_id = e.id AND #{filter_estates}) as service_providers,"+
"(select count(*) from approved_vendors av where av.estate_id = e.id AND #{filter_estates})as vendors"+
" FROM estates e LEFT JOIN governing_bodies g on e.governing_body_id = g.id AND #{filter_estates} "
and i am getting an error.
(Mysql::Error: Subquery returns more than 1 row: SELECT (e.name) as estate_name, g.name as governing_body,(select count() from stands s where s.estate_id = e.id AND e.id IS NOT NULL) as total_stands, (select e.active from estates e where e.active = true AND e.id IS NOT NULL) as estate_status, (select count() from services sp where sp.estate_id = e.id AND e.id IS NOT NULL) as service_providers,(select count(*) from approved_vendors av where av.estate_id = e.id AND e.id IS NOT NULL)as vendors FROM estates e LEFT JOIN governing_bodies g on e.governing_body_id = g.id AND e.id IS NOT NULL ):
and i want to display a all estates which are active and inactive.
please guys,how can i solve this problem. i am using Mysql database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来您的第三行可能有问题:
上面和下面的行使用聚合,因此它们只返回一行,这可能(可能是)返回多行,并且它不知道将哪一个分配给estate_status。
您可能可以将该行更改为:
It looks like you might have a problem with your third line:
The lines above and below that use an aggregate, so they only return one row, this could (probably is) returning multiple rows and it doesn't know which one to assign to estate_status.
You could probably just change that line to:
我当然不熟悉该表,因此我现在可以给您的最佳答案是为什么该查询不起作用。
select e.active from Estates e where e.active = true AND #{filter_estates}) asestate_status
该行返回不止一行,但您不能在那里执行此操作。 请注意,其他函数使用聚合函数,因此它们只返回一行。
哦,我不太使用 My SQL,但是在 T-SQL 中我们经常做 max(e.active) 之类的事情,或者可能放一个 top 1 (我认为这是我的 sql 中的 Limit 1)
I certainly am not familiar with the table so the best answer I can give you right now is why that query doesn't work.
select e.active from estates e where e.active = true AND #{filter_estates}) as estate_status
that line returns more than one row, which you can't do there. Notice your other ones use aggegrate functions so they do only return one row.
Oh, I don't use My SQL much, but in T-SQL we often do things like max(e.active) or perhaps put a top 1 (which I think is Limit 1 in my sQL)
您的子查询
返回多个值。
如果可以的话,请使用“TOP 1”,例如:
Your subquery
returns more than one value.
If you can, use 'TOP 1' like :
SELECT 子句中的子查询必须仅返回 1 行和 1 列才能明确。 这件作品产生多于 1 行:
将其更改为
A subquery in SELECT clause must return just 1 row and 1 column to be unambigous. This piece produces more than 1 row:
change it to