简体   繁体   中英

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

I'm attempting to only SELECT row(s) where the expiry_date (of type timestamp with timezone) is no more than 30 days less than the current_date (ie the expiry date is 30 days).

I'd thought this was working (note: write a better test case!) but apparently not. Some testing in the CLI isn't returning the expected results.

The first query grabs a single row (a detail page) and the second grabs multiple rows (paginated views).

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

(given an id ($1), return the row if the expiry date isn't greater than 30 ($2) days).

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

(given an offset ($2), return the rows that haven't expired, up to limit ($3). In the default case, these are 0 and 15, respectively).

  • When running the first query on a row that has an expiry_date in the past (ie 2014-03-08 05:15:44.236747+00) it's still returning the row

     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) 
  • When running the second query I'm getting a mix of both expired rows and unexpired rows.

     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) 

For context, the dates are generated in my web application as UTC timestamps; the expiry_date is set prior to insert by adding X days (nominally 30) to the created_date timestamp.

What am I missing here?

If you want expired rows only, you have to indicate that in the query. In your example you select rows that have an expiry date that is later than 30 days before now; all future expiries are therefore also returned. Try this:

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

The results are exactly what I would expect.

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

I think your problem is one in the definition of the query. You say:

Where the expiry_date (of type timestamp with timezone) is no more than 30 days less than the current_date (ie the expiry date is 30 days).

That's not all that clear. What exactly do you want? Rows that are expired by less than thirty days? Rows that are not expired, but due to expire within 30 days? Rows that won't expire within 30 days?

Your current query finds rows that have expired or will expire within 30 days, ie where the current date minus thirty days is less than the expiry date.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM