ruby on rail数据库查询

来源:互联网 发布:中国家庭暴力数据 编辑:程序博客网 时间:2024/05/16 13:57

闲来无事,结合以前的代码,总结了ruby on rails的查询方法,方便自己以后查看,也方便后来人,如下,欢迎批评指正

[sql] view plaincopy
  1. 1::simpleDB  
  2.     modules = find(:all, :conditions => ["site_id != '' and next_crawl < ? and next_crawl is not null and next_crawl != 'nil' and active='#{active}' and (in_queue is null or in_queue='nil' or in_queue='false')"Time.now],:order=>"next_crawl")  
  3.     modules = Module.find(:all, :conditions => [" next_snapshot is not null and next_snapshot!='nil' and next_snapshot < ? and priority= ? and ready_for_queue = ? and uri not like 'http://weibo.com%'"Time.now, priority,true], :order=>"next_snapshot",:limit => limit)  
  4.     modules = Module.select(:all, :conditions => ["date > ?""#{(current-20.day).strftime('%Y-%m-%d')}"], :order => 'date desc')  
  5.     modules = Module.find(post_id,:params => {:topic_id => topic_id})  
  6.       
  7. 2::Mysql   
  8.    Mysql使用find 进行查询  
  9.     module = Module.find("id")  Mysql默认 使用id 查询,直接find 即可  
  10.     modules = Module.find(:all, :conditions=>['brand_id = ? and charged >?',@brand_id, 1.months.ago]).uniq 根据条件 查询所有,去重复  
  11.     module = Module.find(:first,:conditions=>["member_id = ? and id = ?",session[:user_id],params[:job_id]])  
  12.     module = Module.find(:all,:conditions=>["id in (?)",job_ids])  in  job_ids 查询  
  13.     module = Module.find(:all, :conditions=>['col_name = ?',col_val], :order=>"created_at") 按照某个字段order  
  14.     Module.find(:all, :conditions => ['member_id = ?', @member.id]).map{|viewview.ft_id} 返回数据,值为ft_id的值  
  15.     modules = Module.find(:all, :limit => limit_no, :where => ["id = '#{id}' and date <= '#{date}'"], :order => [:created_at, :desc]) 排序 等等  
  16.     modules = Module.find(:all, :conditions=>['updated_at > ?'Time.now-20.minute],:order => 'id desc')  
  17.     modules = Module.find(:all,  :joins => [:join_col], :conditions =>['m_id = ?',m_id] )  
  18.     modules = Module.find(:all,:conditions=>["date > ? and site = 'http://s.weibo.com' and keyword != ''",total_day.day.ago],:order=>["keyword asc","date desc"])  
  19.     MemberSite.find(:all,:conditions=>["member_id = ?", @member.id]).group_by{|ms| ms.m_id}  内置方法之排序:group_by() 按照m_id 排序  
  20.     Module.find(:first, :conditions=>['survey_id = ? and name = ?', survey_id, 'STORY'], :select=>'question_id') 什么意思  
  21.     module = Module.find(:first, :order => "create_date, attending DESC")  
  22.   
  23.    Mysql使用类似于jdbc的查询  
  24.     results = ActiveRecord::Base.connection_pool.with_connection do |connection|  
  25.         id = connection.quote(cuser_id)  
  26.             start_at = Date.parse(start_time).to_time.to_i * 1000  
  27.             end_at = Date.parse(end_time).to_time.end_of_day.to_i * 1000  
  28.   
  29.             sql = " select count(*) as num from user_follower where cuser_id = #{id} and user_verified_type != -1 and followed_at >= #{start_at} and followed_at <= #{end_at} "  
  30.   
  31.             connection.execute(sql)  
  32.         end  
  33.   
  34.     result = ActiveRecord::Base.connection_pool.with_connection do |connection|  
  35.               sql = "select count(user_id) as user_id_count from user_follower where cuser_id = '#{cuser_id}'"  
  36.               connection.execute(sql)  
  37.          end  
  38.          @saved_followers_count = result.first[0] || 0  
  39.   
  40.     results = ActiveRecord::Base.connection_pool.with_connection do |connection|  
  41.             id = connection.quote(cuser_id)  
  42.             sql = " select year, month, day, post_type, count(*) as num , group_concat(concat(post_id,':',topic_id) SEPARATOR ',' ) as ids "+  
  43.                 " from posts_group_by_type_date_time where cuser_id = #{id} and " + generate_date_sql(startTime, endTime) + " group by year,month,day,post_type "  
  44.   
  45.             connection.execute(sql)  
  46.         end  
  47.         把结果当做array返回 to_a  
  48.         if(results.count == 0)  
  49.             return []  
  50.         else  
  51.             return results.to_a  
  52.         end  
  53.   
  54.   
  55.     mysql 使用where查询  
  56.     Scope.where({:col => "XXX"}) 最简单的where查询  
  57.     @industries = Scope.where({:module_type => "industry"}).map{|scope| scope.scope} 上面有介绍  
  58.     @scope = Scope.where({:scope => params["scope"]}).first  返回第一个值  
  59.     Threads.where("scope = ? and period = ?", m.id, period).count 根据条件查询,然后count  
  60.     Analytics.where({:scope => m.id,:topic => 'all', :site_id=>'all',  :date => start_time...end_time}).sum("posts") 另外一种赋值方式  

  1.     Threads.where("scope = ? and topic like ? and period = ?", scope, "%#{topic}%", period).limit(50)  limit查询  





转载至:http://blog.csdn.net/cloudcraft/article/details/9343089

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 海水腐蚀的手机怎么办 买回来海胆苦怎么办 被海胆刺了怎么办 长刺海胆刺伤怎么办 耳钉容易掉怎么办 皮球鱼产卵了怎么办 孩子在家太孤独怎么办 宝宝老是挠头是怎么办 猫脸花浇水多了怎么办 夏天钓鲫鱼小杂鱼闹窝怎么办 家里养花生虫子怎么办 生鸡蛋拌饭怎么办 出柜家里人会怎么办 百灵鸟怕人撞笼怎么办 乌鸫雏鸟撞笼返生怎么办 泥石流来了怎么办教案 墙壁插座不通电怎么办 当前目录不能用怎么办 海鲜过敏怎么办没有药 哺乳后胸变小怎么办 有脑出血前兆该怎么办 体检发现甲状腺结节怎么办 月子里宝宝感冒怎么办 10天新生儿感冒怎么办 20天新生儿感冒怎么办 新生儿20天鼻塞怎么办 25天新生儿感冒怎么办 42天新生儿感冒怎么办 上呼吸道感染怎么办比较好 小猫咪太调皮怎么办 水晶彩泥弄到衣服上怎么办 进境动植物检疫许可怎么办 跳舞不会听拍子怎么办 税盘丢了注销公司怎么办 认缴资金不到位怎么办 同一单元有凶宅怎么办 有地皮没房产证怎么办 社保资金被侵吞怎么办? 集体计件手脚慢怎么办 发票当月没用完怎么办 非工业用地怎么办环评