2014-04-08 23 views
0

如何將以下sql查詢轉換爲Ruby on Rails?如何將SQL查詢轉換爲Ruby on Rails

select health_workers.name,health_workers.surname,clinics.name as clinic,count(observations.id) as count,observations.observation_date 
from health_workers 
left join observations on health_workers.id = observations.health_worker_id, clinics 
where health_workers.clinic_id = clinics.id 
group by health_workers.name,health_workers.surname,clinics.name,observations.observation_date. 

回答

0

你可以做這樣

query = "select health_workers.name,health_workers.surname,clinics.name as clinic,count(observations.id) as count,observations.observation_date 
from health_workers 
left join observations on health_workers.id = observations.health_worker_id, clinics 
where health_workers.clinic_id = clinics.id 
group by health_workers.name,health_workers.surname,clinics.name,observations.observation_date." 


results = ActiveRecord::Base.connection.execute(query) 

,這將讓你的結果