2013-08-16 143 views
4

我有這樣的錯誤可以解釋我做錯了什麼嗎?如果你正在從子查詢超過1排
子查詢返回多於1行sql查詢

(? = (select travel_region_id from relationships where travel_id = travels.id)) 

錯誤

ActiveRecord::StatementInvalid (Mysql::Error: Subquery returns more than 1 row: select count(*) from travel_start_days, cars, travels 
        where travels.id = travel_start_days.travel_id and travels.id = travel.car_id and travel_start_days.day > adddate(curdate(), interval '2' day) and (7 = (select travel_region_id from relationships where travel_id = travels.id)) and '2013-08-16' <= travel_start_days.day): 

更新 WHIS的方法來創建查詢

def conditions 
     where = '' 
     param = [] 
     if @region && @region != '' 
     where += 'travels.region_id = ?' 
     param += [@region.to_i] 
     end 
     if @car && @car != '' 
     where += ' and ' if where != '' 
     where += 'cars.id = ?' 
     param += [@car.to_i] 
     end 
     if @relation && @relation != '' 
     where += ' and ' if where != '' 
     where += '(? = (select travel_region_id from relationships where travel_id = travels.id))' 
     param += [@relation.to_i] 
     end 
     if @start_port && @start_port != '' 
     where += ' and ' if where != '' 
     where += '(? = (select location_id from travel_days where travel_id = travel_start_days.travel_id and day_no = 1 order by arrival asc limit 1))' 
     param += [@start_port.to_i] 
     end 
     return where == '' ? nil : ["travel_start_days.day > adddate(curdate(), interval ? day) and " + where] + [@criteria[5]] + param 
    end 
+2

樣本數據和表結構PLS –

回答

1

在此條件部分的問題:

(7 = (select travel_region_id from relationships where travel_id = travels.id)) 

顯然,這種子查詢返回多個travel_region_id只是IN

(7 IN (select travel_region_id from relationships where travel_id = travels.id)) 
+0

+1更換=:正是我要回答 –

+0

謝謝你這項工作! – Andrew

0


我只添加這個SQL查詢,然後將group by子句添加到子查詢中

SELECT travel_region_id FROM relationships 
    WHERE travel_id = travels.id 
    GROUP BY travel_region_id