嗨,我正在从我的桌子屋中选择我的活动记录,我的所有其他记录都很好,但是活动记录给我错误,我的代码是
@query = Estate.find_by_sql“SELECT(e.name)as estate_name,g.name as governance_body,”+ “(从stand s中选择count(),其中s.estate_id = e.id AND#{filter_estates})为total_stands,”+ “(选择e.active从estates e,其中e.active = true AND#{filter_estates})作为estate_status,”+ “(从服务sp中选择count(),其中sp.estate_id = e.id AND#{filter_estates})as service_providers,”+ “(从approved_vendors av中选择count(*),其中av.estate_id = e.id AND#{filter_estates})作为供应商”+ “来自庄园e LEFT JOIN govern_bodies g on e.governing_body_id = g.id AND#{filter_estates}”
我收到了错误。
(Mysql ::错误:子查询返回多行:SELECT(e.name)作为estate_name,g.name作为gover_body,(从stand s中选择count(),其中s.estate_id = e.id并且e.id IS NOT NULL)作为total_stands,(选择e.active从estates e,其中e.active = true和e.id IS NOT NULL)作为estate_status,(从服务sp中选择count()sp where sp .estate_id = e.id AND e.id IS NOT NULL)as service_providers,(来自approved_vendors av的select count(*),其中av.estate_id = e.id和e.id IS NOT NULL)作为供应商FROM estates e LEFT JOIN governance_bodies g on e.governing_body_id = g.id AND e.id IS NOT NULL):
我希望显示所有有效和无效的庄园。
请大家,我怎么能解决这个问题。我正在使用Mysql数据库。答案 0 :(得分:3)
看起来你的第三行可能有问题:
(选择e.active从estates e,其中e.active = true AND#{filter_estates})作为estate_status
使用聚合的上方和下方的行,因此它们只返回一行,这可能(可能是)返回多行,并且它不知道将哪一行分配给estate_status。
您可能只需将该行更改为:
e.active as estate_status
答案 1 :(得分:1)
我当然不熟悉这个表,所以我现在能给你的最佳答案就是为什么这个查询不起作用。
从estates e中选择e.active,其中e.active = true AND#{filter_estates})为estate_status
该行返回多行,您无法在那里执行。注意你的其他人使用aggegrate函数,所以他们只返回一行。
哦,我没有太多使用My SQL,但是在T-SQL中我们经常做max(e.active)之类的事情,或者可能会把top 1(我认为我的sQL中的限制1)
答案 2 :(得分:0)
您的子查询
(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 ...
答案 3 :(得分:0)
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