Tag: sql

所有在数组中的PostgreSQL

实现子句中最简单,最快速的方法是什么,必须匹配数组中的所有元素 – 使用IN时不仅仅是一个? 毕竟它应该像mongodb的$ all 。 考虑到conversation_users是conversation_id和user_id之间的连接表的群组对话我有类似这样的想法: WHERE (conversations_users.user_id ALL IN (1,2)) 更新 16.07.12 添加有关架构和案例的更多信息: join-table非常简单: Table “public.conversations_users” Column | Type | Modifiers | Storage | Description —————–+———+———–+———+————- conversation_id | integer | | plain | user_id | integer | | plain | 对话有很多用户,用户属于许多对话。 为了找到对话中的所有用户,我正在使用此连接表。 最后,我试图找出一个ruby on rails scope ,根据它的参与者找到我的对话 – 例如: scope :between, ->(*users) { […]

在PostgreSQL中执行此小时的操作查询

我在RoR堆栈中,我必须编写一些实际的SQL来完成所有“打开”记录的查询,这意味着当前时间在指定的操作时间内。 在hours_of_operations表中,两个integer列opens_on和closes_on存储工作日,两个time字段opens_at和closes_at存储一天中的相应时间。 我做了一个查询,将当前日期和时间与存储的值进行比较,但我想知道是否有一种方法可以转换为某种类型的日期类型并让PostgreSQL完成其余的工作? 查询的内容是: WHERE ( ( /* Opens in Future */ (opens_on > 5 OR (opens_on = 5 AND opens_at::time > ‘2014-03-01 00:27:25.851655’)) AND ( (closes_on 5) OR ((closes_on = opens_on) AND (closes_at::time ‘2014-03-01 00:27:25.851655’)) OR ((closes_on = 5) AND (closes_at::time > ‘2014-03-01 00:27:25.851655’ AND closes_at::time < opens_at::time))) OR /* Opens in Past */ […]

连接集必须包含所有值但可能包含更多值的SQL

我有三个表格, sports和offers_sports表offers_sports 。 class Offer < ActiveRecord::Base has_and_belongs_to_many :sports end class Sport < ActiveRecord::Base has_and_belongs_to_many :offers end 我想选择包含一系列运动名称的优惠。 他们必须包含所有的sports但可能有更多。 让我们说我有这三个优惠: light: – “Yoga” – “Bodyboarding” medium: – “Yoga” – “Bodyboarding” – “Surfing” all: – “Yoga” – “Bodyboarding” – “Surfing” – “Parasailing” – “Skydiving” 鉴于arrays[“Bodyboarding”, “Surfing”]我想要得到medium而不是light 。 我尝试过这个答案,但结果是零行: Offer.joins(:sports) .where(sports: { name: [“Bodyboarding”, “Surfing”] }) .group(“sports.name”) […]

如何通过ruby中的聚合函数来订购表?

我在Tender模型中有2个属性opening_date和closing_date 。 在索引页面上,我显示的是剩余日期( closing_date-Date.today )。 但是,剩余日期不会保存在数据库中,但我想按剩余日期排序并显示表中的数据。 我该怎么用? 那有一个聚合函数吗? 我使用的是Ruby 1.9.2和Rails 3.2.2 // TendersController def index @tenders= Tender.where(“company_id= ? “, current_user.company.id).order(‘closing_date’) /* Here I am querying the tender by closing_date but I dont want to do that. What I wanted is I just want to order by (closing_date-Date.today) which is not in the database but should […]

更快的数据库WHERE for Rails可用性日历

我正在建立一个Ruby on Rails宿舍床预订系统,我正在寻找一种更好的方式来加载我的可用性日历。 下面的代码非常适合显示床的可用性(参见附图),但我觉得多个WHERE数据库调用在服务器上过于繁琐,必须有更快的方法来处理数据。 楷模: user.rb bed.rb(user_id,name) guest.rb(user_id,姓名,电话,地址,电子邮件) reservation.rb(user_id,bed_id,guest_id,date) 用户是宿舍业主。 他们创造了与他们的生意一样多的床并给他们起了一个名字(Bed#1,Bed#2,Twin-Attic,Master-2nd Floor等)。 每个用户都有很多客人,这些人是在特定日期在宿舍睡觉的人。 使用simple_calendar gem,我获得当前月份的所有预订并处理它们以显示每周视图。 排序逻辑: 步骤1.在一周的第一天通过current_user的床进行迭代。(simple_calendar提供每天的预订) 步骤2.如果床有当天的预订(!=无),请提供床名和预订的客人姓名。 步骤3.如果床没有预订,请将床名和“空”。 步骤4.完成一周的第一天后,转到第二天并重复。(simple_calendar处理此事) 正如我所说的,下面的代码有效,但是当用户有50张床位时,每天会拨打50个WHERE电话,因此对于7天日历会有350个(如果需要找到客人的姓名,则会有更多)。 如果您需要更多信息或解释,请与我们联系。 谢谢! = week_calendar events: @monthly_reservations, attribute: :date do |date, appts| – current_user.beds.each do |bed| – if bed.reservations.where(date: date).first != nil – guest_name = bed.reservations.where(date: date).first.guest.abbreviated_name %p{style: “border: 1px solid black; padding: 5px; font-size: […]

Ruby On Rails检索mysql数据库数据’a’不同’b’

我的数据库有一个设备读取列表, 列是id,device_id,device_reading,update_time 如何为每个设备选择所有最新读数? 这意味着,我首先需要根据update_time对数据进行排序 然后我需要过滤数据库使用一些独特的方法(唯一的device_id) 那么我可以从刚刚过滤的行中检索device_reading。 也就是说,从表中检索列a,其中b是不同的 我该怎么做到这一点? 在铁轨上的ruby 提前致谢

如何用ruby修改.sql文件?

我有一个sql文件(alice.sql),在编辑器中看起来像这样 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_dg_object_extern_pub_dg_extern_pub_status]’) and OBJECTPROPERTY(id, N’IsForeignKey’) = 1)ALTER TABLE [dbo].[dg_object_extern_pub] DROP CONSTRAINT FK_dg_object_extern_pub_dg_extern_pub_status GO 如果我将该文件加载到irb中,它看起来像这样 f = File.open(“alice.sql”).readlines 它看起来像这样:( => [“\377\376i\000f\000 \000e\000x\000i\000s\000t\000s\000 \000(\000s\000e\000l\000e\000c\000t\000 \00 我想搜索并替换文件中的一些字符串,但现在这似乎不可能 有任何想法吗?

如何计算用户对我的特定项目(Rails)所做的所有喜欢

我为新手问题道歉,我对Rails和SQL相对较新。 我正在尝试计算用户对我的特定项目所做的所有喜欢。 在SO社区和Treehouse Coding的帮助下,我能够显示哪些用户喜欢我的特定项目。 此外,我能够研究如何在我的控制器中使用.join显示我收到的所有喜欢(来自Lisa,James,Harry等)。 现在我想显示每个特定用户的计数。 所以,例如,如果詹姆斯喜欢我的4件物品,我想在詹姆斯旁边展示4件。 我在下面列出了所有相关代码,非常感谢你们! Index.html.erb My Fans – Items_controller def index @items = Item.order(“created_at DESC”) if current_user.present? @likers = current_user.items.map(&:likes).flatten.map(&:user).flatten @likersnumero = current_user.items.joins(:likes).map(&:user).count end end item.rb的 class Item true end users.rb的 class User < ApplicationRecord has_many :likes def likes?(post) post.likes.where(user_id: id).any? end #Tried using def total_likes in my index.html.erb using but […]

rake db出错:使用tiny_tds和sqlserver adapter进行迁移

当我尝试使用rake db:migrate此错误: LoadError: cannot load such file — tiny_tds/tiny_tds D:/myrailsapp/config/application.rb:7:in `’ D:/myrailsapp/Rakefile:4:in `require_relative’ D:/myrailsapp/Rakefile:4:in `’ LoadError: 126: The specified module could not be found. – D:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/tiny_tds-1.1.0-x86-mingw32/lib/tiny_tds/2.2/tiny_tds.so D:/myrailsapp/config/application.rb:7:in `’ D:/myrailsapp/Rakefile:4:in `require_relative’ D:/myrailsapp/Rakefile:4:in `’ (See full trace by running task with –trace) 这是我的database.yml文件: # # Ensure the SQLite 3 gem is defined in your Gemfile # gem […]

通过在其他表上计数的rails命令

我正在从Hartl教程中为twitter应用添加测验function,并拥有以下模型: 用户与教程几乎相同: class User < ActiveRecord::Base has_many :followed_users, through: :relationships, source: :followed has_many :takens, dependent: :destroy has_many :questions, through: :takens end 使用的是用户ID的问题ID表: class Taken < ActiveRecord::Base belongs_to :user belongs_to :question end 问题没什么有趣的: class Question < ActiveRecord::Base attr_accessible :category, :correct, :option1, :option2, :option3, :qn end 我希望能够按照他们所进行的测试次数来显示follow_users和followers。 在控制台中,这可以通过: User.find_by_id(1).question_ids.count 然后我可以这样做: User.find_by_id(1).followers.first.question_ids.count 在控制台中获取单个关注者的计数。 我觉得我差不多了。 如何通过’takens’计数对关注者和follow_users进行排序? (我也在看cache_count,起初看起来很有希望,但可能不是我需要的……)