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

后端 未结 6 949
日久生厌
日久生厌 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条回答
  •  清酒与你
    2020-12-16 03:01

    This answer applies to MySQL

    ==> lower than 8.0

    SET @row_number = 0; 
    SELECT 
        (@row_number:=@row_number + 1) AS num, 
        myColumn.first, 
        myColumn.second
    FROM
        myTable
    ORDER BY myColumn.first, myColumn.second   
    

    source: http://www.mysqltutorial.org/mysql-row_number/


    ==> greater than 8.0

    Please see MySQL ROW_NUMBER() function manual as I did not test. But it seems this function is prefered.

提交回复
热议问题