【问题标题】:How to get duplicate month and year matched record in rails?如何在rails中获得重复的月份和年份匹配记录?
【发布时间】:2019-05-18 16:55:45
【问题描述】:

我想从表中获取重复的月份记录 我在 start_date 列中有叶子表,我的数据库中有很多记录 https://imgur.com/a/jdyxTo1 示例我在表中有 3 条重复记录 返回重复记录#08.01.2019,31.01.2019,25.01.2019 如何做到这一点?

class LeaveController < ApplicationController
  def new
    #your code here 
  end
end
  • 我的模型名离开我的表名离开

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-5


    【解决方案1】:

    在 SQLite 上,你不能。支持的 SQL 功能非常少,您最好有一个本地 postgres/mysql 服务器 - 与您将在生产中使用的服务器和版本相同。

    在真正的 SQL 数据库中,您有 EXTRACT(YEAR_MONTH from date) 函数,并且可以将它与 GROUP BY 一起使用。

    这可能是正确的 SQL,您可以将其与普通的 Leave.connection.execute(...) 一起使用:

    SELECT
      GROUP_CONCAT(`leaves`.`id` SEPARATOR ',') AS ids,
      GROUP_CONCAT(`leaves`.`start_date` SEPARATOR ',') AS start_dates
    FROM `leaves`
    GROUP BY EXTRACT(YEAR_MONTH from `leaves`.`start_date`)
    HAVING COUNT(`leaves`.`id`) > 1
    

    有了图片中的数据,你会得到如下结果:

    ids               | start_dates
    ------------------+---------------------------------
    5,6,8             | 2019-01-08,2019-01-31,2019-01-25
    1,2,3,4           | ...
    

    并且不与他人共享月份的假期将没有条目。

    【讨论】:

      【解决方案2】:

      当您尝试在单个数组中查找所有月份时,您无法按月份查找重复的日期 下面可以为您提供一个对象数组,其中您的月份编号和该月份编号的重复日期将在那里

      对于 MySQL / PostgresQL

      class LeaveController < ApplicationController
        def new
          @result = []
          12.times do |i|
            month_data = {
              month_number: i,
              duplicate_dates: Leave.where("extract(month from start_date) = ?", i).pluck(:start_date)
            }
            @result.push month_data
          end
          #Print and see the result
          p @result
        end
      end
      

      SQLite 更新

      由于您使用的是 sqlite,并且不支持上述语法,所以这里有一个相同的方法:

      # In your model you can write a class method so you don't have to write all that
      #   logic in your controller and make it reusable
      class Leave < ActiveRecord::Base
        def self.duplicate_dates(current_month)
          query = "SELECT * FROM leaves WHERE strftime('%m', start_date) = #{current_month}"
          self.find_by_sql query
        end
      end
      
      class LeaveController < ApplicationController
        def new
          @result = []
          ('01'..'12').to_a.each do |i|
            # Get all the records with duplicate month
            duplicate_data = Leave.duplicate_dates(i)
            # The above will return a regular array and not an activerecord association,
            #   so pluck can't be used on it
      
            # To get only the dates you can use map 
            duplicate_dates = duplicate_data.map {|i| i.start_date}
      
            month_data = {
              month_number: i,
              duplicate_dates: duplicate_dates
            }
            @result.push month_data
          end
          #Print and see the result
          p @result
        end
      end
      

      【讨论】:

      • 我面临错误 SQLite3::SQLException: near "from": syntax error: SELECT "leaves"."start_date" FROM "leaves" WHERE (extract(month from start_date) = 0)跨度>
      • 我也更新了 SQLite 的代码。请让我知道这是否有效
      猜你喜欢
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多