0

このようにRailsでサブクエリを書く方法は?:

select
    m.field1,
    m.field2,
    (select count(*) from details1 d1 where d1.id=m.field3) as count1,
    (select count(*) from details2 d2 where d2.id=m.field4) as count2,
    (select count(*) from details3 d3 where d3.id=m.field5) as count3
from
    master m

データベースサーバーはPostgreSQLです。

4

1 に答える 1

3
class Master < ActiveRecord::Base
  set_table_name "master"
end
masters = Master.select(%Q"master.field1, master.field2,
(select count(*) from details1 d1 where d1.id=m.field3) as count1,
(select count(*) from details2 d2 where d2.id=m.field4) as count2,
(select count(*) from details3 d3 where d3.id=m.field5) as count3").all
于 2012-05-17T19:16:51.617 に答える