MySQL query to select results with auto increment as a new column added in the result

前端 未结 2 911
一向
一向 2020-12-03 00:23

I have a student\'s table with the following fields:

student(student_id, student_name, student_avg)

I need to write a query in MySQL which

相关标签:
2条回答
  • 2020-12-03 00:56

    Try this on

    SELECT  @s:=@s+1 serial_number,student_id,student_name,student_avg
    FROM    students,
            (SELECT @s:= 0) AS s
    WHERE
    student_avg > 4;
    

    https://stackoverflow.com/a/11096550/1423506

    0 讨论(0)
  • 2020-12-03 00:58
    SET @serial=0;
    SELECT @serial := @serial+1 AS `serial_number`, `column_name` FROM `table_name`;
    

    In your particular case:

    SET @serial=0;
    
    SELECT 
       @serial := @serial+1 AS `serial_number`, 
       `student_id`, 
       `student_name`, 
       `student_avg`
    FROM 
       `students`
    WHERE
       `student_avg` > 4;
    
    0 讨论(0)
提交回复
热议问题