为什么MySql给出“子查询返回超过1行” 错误?

发布于 2024-07-20 07:39:43 字数 1449 浏览 8 评论 0原文

大家好,我正在从我的表格中选择我的活动记录,我的所有其他记录都很好,但是活动记录给了我错误,我的代码是

@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 技术交流群。

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

发布评论

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

评论(4

七禾 2024-07-27 07:39:43

看来您的第三行可能有问题:

(从 Estates e 中选择 e.active,其中 e.active = true AND #{filter_estates})作为estate_status

上面和下面的行使用聚合,因此它们只返回一行,这可能(可能是)返回多行,并且它不知道将哪一个分配给estate_status。

您可能可以将该行更改为:

e.active 作为estate_status

It looks like you might have a problem with your third line:

(select e.active from estates e where e.active = true AND #{filter_estates}) as estate_status

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:

e.active as estate_status

失退 2024-07-27 07:39:43

我当然不熟悉该表,因此我现在可以给您的最佳答案是为什么该查询不起作用。

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)

英雄似剑 2024-07-27 07:39:43

您的子查询

(SELECT e.active FROM estates e WHERE   ...) AS estate_status  

返回多个值。

如果可以的话,请使用“TOP 1”,例如:

SELECT e.name AS estate_name   ,
       g.name AS governing_body,
       (SELECT COUNT(*) FROM stands s            WHERE   ...) AS total_stands,
       (SELECT TOP 1 e.active FROM estates e     WHERE   ...) AS estate_status,
       (SELECT COUNT(*) FROM services sp         WHERE   ...) AS service_providers,
       (SELECT COUNT(*) FROM approved_vendors av WHERE   ...) AS vendors
FROM   estates e
LEFT 
JOIN   governing_bodies g ON  e.governing_body_id = g.id
                          AND ...

Your subquery

(SELECT e.active FROM estates e WHERE   ...) AS estate_status  

returns more than one value.

If you can, use 'TOP 1' like :

SELECT e.name AS estate_name   ,
       g.name AS governing_body,
       (SELECT COUNT(*) FROM stands s            WHERE   ...) AS total_stands,
       (SELECT TOP 1 e.active FROM estates e     WHERE   ...) AS estate_status,
       (SELECT COUNT(*) FROM services sp         WHERE   ...) AS service_providers,
       (SELECT COUNT(*) FROM approved_vendors av WHERE   ...) AS vendors
FROM   estates e
LEFT 
JOIN   governing_bodies g ON  e.governing_body_id = g.id
                          AND ...
夏日落 2024-07-27 07:39:43

SELECT 子句中的子查询必须仅返回 1 行和 1 列才能明确。 这件作品产生多于 1 行:

"(select e.active from estates e where e.active = true AND #{filter_estates}) as estate_status

将其更改为

"(select first(e.active) from estates e where e.active = true AND #{filter_estates}) as estate_status

A subquery in SELECT clause must return just 1 row and 1 column to be unambigous. This piece produces more than 1 row:

"(select e.active from estates e where e.active = true AND #{filter_estates}) as estate_status

change it to

"(select first(e.active) from estates e where e.active = true AND #{filter_estates}) as estate_status
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文