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? .

4

2 回答 2

1

这个怎么样:

SELECT *, 25 AS current_query_offset
FROM test_table
LIMIT 0 OFFSET current_query_offset
于 2013-01-17T13:20:08.947 回答
1

如果我正确理解您想要实现的目标(即:选择包含常量值的命名列以及任何其他列),这将起作用:

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

由于您已经25为您的 sql 代码提供了一次值,所以我不明白您为什么不应该第二次这样做。

于 2013-01-17T15:48:57.157 回答