Pure-SQL Technique for Auto-Numbering Rows in Result Set

前端 未结 9 1131
误落风尘
误落风尘 2020-12-29 00:20

I\'m looking for a way to sequentially number rows in a result set (not a table). In essence, I\'m starting with a query like the following:

SELECT         


        
9条回答
  •  执念已碎
    2020-12-29 00:47

    AFAIK, there's no "standard" way.

    MS SQL Server has row_number(), which MySQL has not.

    The simplest way to do this in MySQL is

    SELECT a.*, @num := @num + 1 b from test a, (SELECT @num := 0) d;

    Source: comments in http://www.xaprb.com/blog/2006/12/02/how-to-number-rows-in-mysql/

提交回复
热议问题