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

前端 未结 8 1744
轻奢々
轻奢々 2020-11-21 23:57

Is there any way I can get the actual row number from a query?

I want to be able to order a table called league_girl by a field called score; and return the username

8条回答
  •  终归单人心
    2020-11-22 00:38

    Here comes the structure of template I used:

      select
              /*this is a row number counter*/
              ( select @rownum := @rownum + 1 from ( select @rownum := 0 ) d2 ) 
              as rownumber,
              d3.*
      from 
      ( select d1.* from table_name d1 ) d3
    

    And here is my working code:

    select     
               ( select @rownum := @rownum + 1 from ( select @rownum := 0 ) d2 ) 
               as rownumber,
               d3.*
    from
    (   select     year( d1.date ), month( d1.date ), count( d1.id )
        from       maindatabase d1
        where      ( ( d1.date >= '2013-01-01' ) and ( d1.date <= '2014-12-31' ) )
        group by   YEAR( d1.date ), MONTH( d1.date ) ) d3
    

提交回复
热议问题