【问题标题】:Get records within specific year and month获取特定年份和月份内的记录
【发布时间】:2017-10-08 11:46:48
【问题描述】:

我有这个类方法来过滤属于特定年份和月份的记录

@posts = Post.filtered(params).published

  def self.filtered (params)
    unless params[:year].blank? && params[:month].blank?
      year = params[:year].to_i
      month = params[:month].to_i
      return where(created_at: Date.new(year, month, 1)..Date.new(year, month, -1))
    end
    self
  end

生成:

SELECT  `posts`.* 
FROM `posts` 
WHERE (`posts`.`created_at` BETWEEN '2017-09-01' AND '2017-09-30') -- AND ...

rails c 我做了Post.pluck('id, created_at') 并得到了这些结果

SELECT id, created_at FROM `posts` ORDER BY created_at DESC
=> [[21, Fri, 06 Oct 2017 22:10:00 UTC +00:00],
 [22, Sat, 30 Sep 2017 22:10:00 UTC +00:00], # September records
 [7, Fri, 08 Sep 2017 19:48:14 UTC +00:00],  # is where I get wrong number of records
 [4, Tue, 29 Aug 2017 20:55:19 UTC +00:00],
 [9, Wed, 16 Aug 2017 02:32:24 UTC +00:00],
 [19, Tue, 15 Aug 2017 09:57:58 UTC +00:00],
 [14, Mon, 14 Aug 2017 20:03:49 UTC +00:00],
 [18, Mon, 14 Aug 2017 16:04:40 UTC +00:00],
 [2, Sat, 12 Aug 2017 15:44:21 UTC +00:00],
 [15, Sun, 06 Aug 2017 22:36:10 UTC +00:00],
 [10, Sat, 22 Jul 2017 23:17:41 UTC +00:00],
 [12, Fri, 21 Jul 2017 06:38:29 UTC +00:00],
 [17, Sat, 15 Jul 2017 06:49:25 UTC +00:00],
 [1, Fri, 14 Jul 2017 22:18:51 UTC +00:00],
 [5, Tue, 11 Jul 2017 03:31:57 UTC +00:00],
 [16, Sun, 09 Jul 2017 06:17:50 UTC +00:00],
 [13, Sat, 08 Jul 2017 09:49:45 UTC +00:00],
 [20, Sat, 08 Jul 2017 06:34:16 UTC +00:00],
 [3, Thu, 06 Jul 2017 00:12:23 UTC +00:00],
 [8, Sun, 02 Jul 2017 20:28:49 UTC +00:00],
 [11, Fri, 23 Jun 2017 04:03:01 UTC +00:00],
 [6, Wed, 21 Jun 2017 07:02:12 UTC +00:00]]

我尝试按每个月的所有记录进行过滤,但我唯一弄错的是属于 9 月的记录,我只得到一条记录而不是两条。

【问题讨论】:

  • 也在此处发布SHOW CREATE TABLE posts 输出。

标签: mysql sql ruby-on-rails scope


【解决方案1】:
Select * from dual 
where created_at BETWEEN '2017-09-01' AND '2017-09-30'

可以读作:

Select * from dual 
where created_at >= '2017-09-01 00:00:00' 
    AND created_at <= '2017-09-30 00:00:00'

并且30 Sep 2017 22:10:00 不小于或等于2017-09-30 00:00:00...

编辑:

SQL Fiddle

MySQL 5.6 架构设置

CREATE TABLE t
    (`d` datetime)
;

INSERT INTO t
    (`d`)
VALUES
    ('2017-09-30 00:00:00'),
    ('2017-09-30 02:02:02'),
    ('2017-09-20 12:12:12'),
    ('2017-09-08 21:21:21'),
    ('2017-09-08 00:00:00')
;

查询 1

select date_format(d,'%Y-%m-%d %k:%i:%s') h from t
where d between '2017-09-08' and '2017-09-30'
order by d

Results

|                   h |
|---------------------|
|  2017-09-08 0:00:00 |
| 2017-09-08 21:21:21 |
| 2017-09-20 12:12:12 |
|  2017-09-30 0:00:00 |

查询 2

select date_format(d,'%Y-%m-%d %k:%i:%s') h from t
where d between '2017-09-08' and '2017-09-30 23:59:59'
order by d

Results

|                   h |
|---------------------|
|  2017-09-08 0:00:00 |
| 2017-09-08 21:21:21 |
| 2017-09-20 12:12:12 |
|  2017-09-30 0:00:00 |
|  2017-09-30 2:02:02 |

【讨论】:

  • 我也试过where(created_at: Date.new(year, month, 1)...Date.new(year, month, -1)),它会生成WHERE (posts.created_at` >= '2017-09-01' AND posts.created_at
  • @Lykos created_at &lt; '2017-09-30' = created_at &lt; '2017-09-30 00:00:00' 你的记录在2017-09-30 22:10:10 所以是一样的
  • 啊等等,你的意思是应该有&lt;=,对吗?
  • 有没有办法在范围内指定这个,还是在这种情况下我需要创建两个不同的where 语句?
  • @Lykos 这与&lt;= 无关,而是'2017-09-30' 不是日期,而是日期时间,因此是'2017-09-30 00:00:00' 的时间。在该时间之后您无法匹配当天的任何内容,您需要指定 23:59:59 时间以匹配全天,或者将日期时间四舍五入(不利于性能)
【解决方案2】:

试试……

def self.filtered(params)
  if params[:year].present? && params[:month].present?
    year = params[:year].to_i
    month = params[:month].to_i
    return where(created_at: Date.new(year, month, 1)..Date.new(year, month + 1, 1).to_time - 1.second)
  end
  self
end

我所做的只是将您的结束日期移到下月初,因此您正在寻找大于 9 月 1 日的午夜和小于 10 月 1 日的午夜。

【讨论】:

  • 不好,BETWEEN 包含在内:您将匹配 2017-10-01 00:00:00
  • 已编辑以适合您的参数 B.
  • 是的,更好,OP选择DateTime.new(year, month, -1).end_of_day但它是一样的,采取+1 ;)
  • 谢谢 B。我确实喜欢你可以按 Date.new(year,month,-1) 拉出月末。相当时髦。没有意识到像 end_of_year 这样的 end_of_day。那会很有意义。每天学些新东西。 ?
  • @Blag 刚刚玩了 Time.new(2017,9,1,0,0).end_of_month 并获得了 9 月的最后一秒。他们接下来会怎么想? :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2017-01-19
相关资源
最近更新 更多