Can I get the position of a record in a SQL result table?

后端 未结 6 863
日久生厌
日久生厌 2020-12-16 02:35

If I do something like

SELECT * FROM mytable ORDER BY mycolumn ASC;

I get a result table in a specific order.

Is there a way in SQL

6条回答
  •  -上瘾入骨i
    2020-12-16 02:57

    Unfortunately you cannot get "the position of a row in a table".

    The best you can get, using ORDER BY and a variant of the ROW_NUMBER construct (depends on the database engine in use), is the position of a row in the resultset of the query executed.

    This position does not map back to any position in the table, though, unless the ORDER BY is on a set of clustered index columns, but even then that position might be invalidated the next second.

    What I would like to know is what you intended to use this "position" for.

提交回复
热议问题