为什么第二次运行select查询后返回NULL

时间:2016-11-26 14:05:28

标签: mysql sql phpmyadmin

我有选择查询,选择学生分数,并从最高到最低排名,这个mysql查询第一次工作,但是当我第二次在phpmyadmin上运行时,它返回NULL,它应该显示在学生的等级,下面是mysql查询;

SELECT @rownum := @rownum + 1 AS rank
     , student_name
     , avga 
  FROM `averaga` 
 WHERE class="Form 1A" 
 ORDER 
    BY avga DESC

这是查询结果的部分快照 part snapshot of the results from query results

1 个答案:

答案 0 :(得分:2)

您需要初始化变量。我通常在查询中执行此操作:

SELECT (@rownum := @rownum + 1) AS rank, student_name, avga
FROM `averaga` a CROSS JOIN
     (SELECT @rownum := 0) params
WHERE class = 'Form 1A'
ORDER BY avga DESC;