WHERE expiry_date> (current_date - 30 * interval'1天')

时间:2014-03-18 23:51:11

标签: sql postgresql

我只尝试SELECT行,其中expiry_date(带时区的时间戳类型)比current_date少30天(即有效期为30天)。

我认为这是有效的(注意:写一个更好的测试用例!)但显然不是。 CLI中的某些测试并未返回预期结果。

第一个查询获取单行(详细信息页面),第二个查询获取多行(分页视图)。

SELECT *
FROM listings
WHERE id = $1 AND expiry_date > (current_date - $2 * interval '1 day')

(给定一个id($ 1),如果失效日期不超过30($ 2)天,则返回该行。

SELECT *
FROM listings
WHERE expiry_date > (current_date - $1 * interval '1 day')
ORDER BY expiry_date DESC OFFSET $2 LIMIT $3

(给定一个偏移量($ 2),返回尚未过期的行,最多限制($ 3)。在默认情况下,分别为0和15)。

  • 在过去有expiry_date的行(即2014-03-08 05:15:44.236747 + 00)上运行第一个查询时,它仍然会返回该行

    SELECT id, expiry_date FROM listings where id = '96144752ad41b6bf' AND expiry_date > (current_date - 30 * interval '1 day');
    ____________________________________________________
    _        id        _          expiry_date          _
    ____________________________________________________
    _ 96144752ad41b6bf _ 2014-03-09 02:33:25.855162+00 _
    ____________________________________________________
    (1 row)
    
  • 运行第二个查询时,我得到了过期行和未过期行的混合。

    SELECT id, expiry_date FROM listings WHERE expiry_date > (current_date - 30 * interval '1 day') ORDER BY expiry_date DESC OFFSET 0 LIMIT 15;
    ____________________________________________________
    _        id        _          expiry_date          _
    ____________________________________________________
    _ 333b0291f8e357b9 _ 2014-04-04 21:40:14.744937+00 _
    _ 975d2a671ab577dd _ 2014-03-26 03:47:21.872271+00 _
    _ af35cbc3b2f6189b _ 2014-03-25 04:43:23.78521+00  _
    _ 3c3658ecceafde4b _ 2014-03-23 14:24:08.023696+00 _
    _ 931c2d5795705b5b _ 2014-03-10 15:43:52.05335+00  _
    _ eddb3711f02a0ad0 _ 2014-03-10 02:38:53.754079+00 _
    _ e2814251618590db _ 2014-03-09 14:06:07.829742+00 _
    _ 96144752ad41b6bf _ 2014-03-09 02:33:25.855162+00 _
    _ 1dbdcd6cb2cc6e7f _ 2014-03-08 05:58:52.121108+00 _
    _ d25d1b06b9e69f3e _ 2014-03-08 05:52:37.887371+00 _
    _ c52aa23d79583033 _ 2014-03-08 05:17:09.683484+00 _
    _ 56e9f4cdca899a40 _ 2014-03-08 05:15:44.236747+00 _
    _ 302f0226b7b33f05 _ 2014-03-08 05:09:43.909115+00 _
    _ edc45b7ca32f69f5 _ 2014-03-07 14:13:51.366852+00 _
    _ 90c1ef396073ff28 _ 2014-03-07 13:04:12.250061+00 _
    ____________________________________________________
    (15 rows)
    

对于上下文,日期在我的Web应用程序中生成为UTC时间戳;在插入之前设置expiry_date,方法是将X天(标称为30)添加到created_date时间戳。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:2)

如果只想要过期的行,则必须在查询中指明。在您的示例中,您选择的有效期为之后的行超过之前的30天;因此,所有未来的到期日也将被退回。试试这个:

SELECT *
FROM listings
WHERE expiry_date > (current_date - $1 * interval '1 day')
  AND expiry_date < now()
ORDER BY expiry_date DESC OFFSET $2 LIMIT $3

答案 1 :(得分:2)

结果正如我所料。

http://sqlfiddle.com/#!15/d110a/3

我认为您的问题是查询定义中的问题。你说:

  

expiry_date(带时区的时间戳类型)不比current_date少30天(即有效期为30天)。

那不是那么清楚。你究竟想要什么?行不到三十天?行未到期,但将在30天内到期?行不会在30天内到期?

您当前的查询发现已过期或将在30天内过期的行,即当前日期减去30天的行数小于失效日期。

regress=> SELECT current_date,  (current_date - 30 * interval '1 day'), expiry_date
regress-> FROM listings
regress-> WHERE id = '96144752ad41b6bf';
    date    |      ?column?       |          expiry_date          
------------+---------------------+-------------------------------
 2014-03-19 | 2014-02-17 00:00:00 | 2014-03-09 10:33:25.855162+08
(1 row)