3

How to get MySQL offset as a column with alias?

What I need is a way to get the current offset as a column in my sql query.

SELECT *, <THE_CURRENT_QUERY_OFFSET> AS current_query_offset
  FROM test_table
 LIMIT 10 OFFSET 25

So, I should have a column named current_query_offset which is 25 in each row.

Note: I don't need the current row position the way it is given in With MySQL, how can I generate a column containing the record index in a table? .

Community
  • 1
  • 1
GeekTantra
  • 10,770
  • 5
  • 39
  • 53

2 Answers2

1

How about this:

SELECT *, 25 AS current_query_offset
FROM test_table
LIMIT 0 OFFSET current_query_offset
cowls
  • 23,287
  • 8
  • 47
  • 77
1

If I understand correctly what you want to achieve (that is: selecting a named column containing a constant value along with any other columns), this will work:

SELECT *, 25 AS current_query_offset
FROM test_table
LIMIT 10 
OFFSET 25;

Since you're supplying the value 25 to your sql code once already, I don't see why you shouldn't do it a second time.

Spiny Norman
  • 8,152
  • 1
  • 32
  • 54