How to put a raw SQL query in Sequel

人走茶凉 提交于 2019-12-04 06:32:22

问题


I am trying to convert SQL code to Seqel to run it from my script. How do I convert this:

select code, count(1) as total 
from school_districts 
group by code order by total desc;

into Sequel? Or, is there a way to pass raw SQL to Sequel? Also the school_districts will be interpolated #{table_name}.


回答1:


You can do it a couple ways:

  1. Use []:

    DB["your sql string"]
    
  2. Use fetch:

    DB.fetch("your sql string")
    



回答2:


DB[:school_districts].select(:code).group_and_count(:code).reverse_order(:count)

is a Sequel way of executing that query. I did not however alias the count column, but I hope you can do with this.

Even though working in Sequel is preferable as it allows you to change DBMs without changing your code I would prefer you use the fetch method.



来源:https://stackoverflow.com/questions/40294289/how-to-put-a-raw-sql-query-in-sequel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!