通过用点对用户进行排名来创建视图

时间:2014-02-07 18:53:30

标签: mysql sql

我正在使用此查询并收到错误#1351 - View's SELECT contains a variable or parameter

CREATE VIEW v AS
SELECT  name,points,sno ,@rownum := @rownum + 1 AS rank
FROM  `users`,(Select @rownum := 0)
GROUP BY   name,sno
ORDER BY CAST(points AS UNSIGNED) DESC;

如何使用此方法创建视图?我非常感谢任何帮助。谢谢你。

2 个答案:

答案 0 :(得分:1)

假设SNOusers表中实际上是唯一的,您可以在没有变量的视图中执行此操作。我不是说你想要,但你可以:

CREATE VIEW v AS
    SELECT  name,
            (select count(*)
             from users u2
             where u2.points > u.points or
                   u2.points = u.points and u2.sno <= u.sno
            ) as rank
    FROM  `users` u;

答案 1 :(得分:0)

正如CREATE VIEW Syntax所述:

  
      
  • SELECT语句不能引用系统或用户变量。
  •