2017-10-08 196 views
1

我有过滤记录这类方法属于特定年份和月份获取特定年份和月份

@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]] 

我已经尝试过滤所有re但是每个月的电话线只有一个我错误的是9月份的记录,我只有一条记录而不是两条记录。

+0

同时发布'SHOW CREATE TABLE posts'输出。 –

回答

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 | 
+0

我也试过'where(created_at:Date.new(year,month,1)... Date.new(year,month,-1))'which which'WHERE('posts'.'created_at'> = '2017-09-01'AND'posts'.'created_at' <'2017-09-30')'但这也会返回1月份的记录 – Lykos

+0

@Lykos'created_at <'2017-09-30'' ='created_at <'2017-09-30 00:00:00''你的记录在'2017-09-30 22:10:10'所以它是一样的 – Blag

+0

啊等待,你的意思是应该有'<=​​',而不是正确的? – Lykos

1

尝试...

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 

我所做的只是将你的结束日期到下月初,所以你正在寻找比九月更大午夜1点,午夜10点1点以下。

+1

不好,'BETWEEN'包含:你会匹配'2017-10-01 00:00:00'。 – Blag

+0

编辑以适合您的参数B. –

+1

是的,更好的是,OP选择'DateTime.new(年,月,-1).end_of_day'但它是一样的,取+1;) – Blag