Generate an integer sequence in MySQL

前端 未结 16 2956
南旧
南旧 2020-11-22 06:47

I need to do a join with a table/result-set/whatever that has the integers n to m inclusive. Is there a trivial way to get that without just buildi

16条回答
  •  执笔经年
    2020-11-22 07:24

    I found this solution on the web

    SET @row := 0;
    SELECT @row := @row + 1 as row, t.*
    FROM some_table t, (SELECT @row := 0) r
    

    Single query, fast, and does exactly what I wanted: now I can "number" the "selections" found from a complex query with unique numbers starting at 1 and incrementing once for each row in the result.

    I think this will also work for the issue listed above: adjust the initial starting value for @row and add a limit clause to set the maximum.

    BTW: I think that the "r" is not really needed.

    ddsp

提交回复
热议问题