无法创建视图

时间:2014-02-11 10:37:30

标签: mysql sql

我收到错误:View的SELECT包含一个带有下面查询的变量或参数。如何创建相同的视图。我非常感谢任何帮助。谢谢你。

CREATE VIEW V AS SELECT @rownum := @rownum + 1 AS rank, name, vote 
FROM uservotes, (SELECT @rownum := 0) t ORDER BY vote DESC

2 个答案:

答案 0 :(得分:0)

declare @rownum int

CREATE VIEW V AS SELECT @rownum := @rownum + 1 AS rank, name, vote 
FROM uservotes, (SELECT @rownum := 0) t ORDER BY vote DESC

答案 1 :(得分:0)

试试这个

CREATE VIEW V AS 
(
SELECT (SELECT 1 + COUNT(*) FROM uservotes where votes < T.votes ) AS NUM, name, votes 
FROM uservotes T ORDER BY votes DESC
)

Fiddle Demo

您可以定义一个变量以获取伪行号功能,因为MySQL没有任何排名功能:

无法在MySQL视图中使用变量

如果这样做,您将收到1351错误,因为you can't use a variable in a view due to designbug/feature behavior is documented here