0

I am using psycopg2.

I want to find the number of rows returned by the query which has offset and limit.

The total number of rows in mytable is 8

I want my result to be 4 as with limit and offset only 4 rows can be returned but I am getting 8. How can I modify the code to get the count of rows fetched with limit and offset i.e. 4.

data_fetch_sql = f"SELECT count(*) FROM mytable limit 4 offset 0"
self.cursor.execute(data_fetch_sql)
data = [x for x in self.cursor]
data[0][0]

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Avenger
  • 793
  • 11
  • 31
  • 1
    This query returns one single row. Why do you think it's four or even eight? Do you fetch the same result set multiple times? – Frank Heikens Apr 19 '22 at 13:07
  • I think the question you are asking is `How do I get a count based on a where clause?`. As it stands your question does not make sense. Or the alternative answer is simply `select * from mytable order by ?? limit 4 offest 0` where ?? is your order by column(s) – Rippo Apr 19 '22 at 13:12

0 Answers0