return mySQL original row number

丶灬走出姿态 提交于 2019-12-02 09:58:33

问题


I want to get the row_number of the original record in the database. Is there any way to retrieve the original row number of a record in mysql? Suppose my statement is

select course_id from course where subject='finance';

there can be multiple records for this query. Suppose they are from row numbers 4, 5, 7, 9 and 10. How can i retrieve these row_numbers from my query?


回答1:


Check: With MySQL, how can I generate a column containing the record index in a table?

SELECT  c.course_id, 
        @curRow := @curRow + 1 AS row_number
FROM    course c
JOIN    (SELECT @curRow := 0) r;

Do note however, this is artificial as the only real 'row number' would be your own primary ID, and every artificial method will change the row number when you change the ORDER clause



来源:https://stackoverflow.com/questions/8607459/return-mysql-original-row-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!