2009-03-12 22 views
0

Benim raylar uygulamasında, alt sorgulara ihtiyacım var çünkü find_by_sql() kullanarak bir sql sorgusu çalıştırıyorum. Bu, ilk veya ikinci sorguyu yaparsam ancak bunları AND ile birlikte eklediğimde çalışır, alt sorguda 1'den fazla satır hakkında şikayet etmeye başlar.Mysql :: Hata: Alt sorgu 1'den fazla satır döndürür:

Geri döndürülen tüm satırların (kayıtların) ölçütlerle eşleşmesini istiyorum. Burada neyin düzeltilmesi/değiştirilmesi gerekiyor? Mysql söylüyorum sadece 1 satır istiyorum? İşte

raylar görüldüğü gibi çıkan bir SQL olduğunu 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) 

cevap

4

Bu hat:

AND (select 0 from status_changes sc 

değil daha dönmek

AND exists (select 0 from status_changes sc 
+2

you da man! - teşekkürler – Streamline

-3

subqueries olmalı Bildiğim kadarıyla 1 satır herhangi bir SQL sunucusu tarafından desteklenmiyor.

+0

Bu saçmalık. – Tomalak

+0

Kimlik seç, kimliğin olduğu konumdaki kullanıcı adı (Active_user'dan id seçin)? – Petruza