Re: Select count with offset returns nothing.

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Tim Uckun <timuckun(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Select count with offset returns nothing.
Date: 2011-08-05 13:30:59
Message-ID: CAOR=d=02vPyvG_-DuNvVX3LKZdrKwkR-YVenHde3bkMLUsdrYw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Aug 5, 2011 at 4:51 AM, Tim Uckun <timuckun(at)gmail(dot)com> wrote:
> I am using a library which is emitting SQL like this  SELECT COUNT(*)
> FROM batches LIMIT 15 OFFSET 15 the library fails because on postgres
> this query returns nothing (not even zero as a result). Presumably it
> returns some valid value on mysql and other databases.
>
> Other than hacking the library is there anything I can do?
>
> Oddly enough SELECT count(*) FROM batches LIMIT 15 returns the full
> count of the table. Presumably the count has to be done on a subquery
> right?

Since select count(*) will return one row, you're limit / offset are
in the wrong place. You want something like:

select count(*) from (select * from batches ordery by something limit
15 offset 15);

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tim Uckun 2011-08-05 13:35:57 Re: Select count with offset returns nothing.
Previous Message Andrew Sullivan 2011-08-05 13:05:51 Re: Temp table visibility