简体   繁体   中英

How to convert SQL query to Ruby on Rails

How do I convert the following sql query to 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.

You can do this way

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)

and this will gives you the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM