Row Rank in a MySQL View

前端 未结 2 1194
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 12:02

I need to create a view that automatically adds virtual row number in the result. the graph here is totally random all that I want to achieve is the last column to be create

2条回答
  •  庸人自扰
    2020-12-03 12:31

    Oracle has a rowid pseudo-column. In MySQL, you might have to go ugly:

    SELECT id,
           variety,
           1 + (SELECT COUNT(*) FROM tbl WHERE t.id < id) as num
      FROM tbl
    

    This query is off the top of my head and untested, so take it with a grain of salt. Also, it assumes that you want to number the rows according to some sort criteria (id in this case), rather than the arbitrary numbering shown in the question.

提交回复
热议问题