MSSQL:必须声明标量变量@rownum

发布于 2024-10-31 17:45:29 字数 578 浏览 0 评论 0原文

我的一个疑问是游戏的排名。

查询如下,SQL Server Management Studio 显示的错误是:“必须声明标量变量@rownum”

有什么问题吗?

多谢!

 $sql1_1     = "SET @rownum := 0";



$sql2_2       =   "SELECT * FROM (
                      SELECT @rownum := @rownum + 1 AS rank, totalpoints, useridFB, game2points
                      FROM theuser ORDER BY game2points DESC
                      ) as result WHERE useridFB=1234";



        mssql_query($sql1_1); 
        $result = mssql_query($sql2_2);

        $row = mssql_fetch_array($result);
        $therank = $row['rank'];

One of my queries is a ranking from a game.

The query is below and the error SQL Server Management Studio shows is: "Must declare the scalar variable @rownum"

what is wrong with it?

Thanks a lot!

 $sql1_1     = "SET @rownum := 0";



$sql2_2       =   "SELECT * FROM (
                      SELECT @rownum := @rownum + 1 AS rank, totalpoints, useridFB, game2points
                      FROM theuser ORDER BY game2points DESC
                      ) as result WHERE useridFB=1234";



        mssql_query($sql1_1); 
        $result = mssql_query($sql2_2);

        $row = mssql_fetch_array($result);
        $therank = $row['rank'];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2024-11-07 17:45:29

您在 SQL Server 中使用 MySql 语法。

使用 row_number() 函数重现当前逻辑。

SELECT *
FROM   (SELECT row_number() OVER (ORDER BY game2points DESC) AS [rank],
               totalpoints,
               useridFB,
               game2points
        FROM   theuser) AS result
WHERE  useridFB = 1234  

或者您可能想要根据您希望如何处理关系来调查排名

You are using MySql syntax in SQL Server.

Use the row_number() function to reproduce your current logic.

SELECT *
FROM   (SELECT row_number() OVER (ORDER BY game2points DESC) AS [rank],
               totalpoints,
               useridFB,
               game2points
        FROM   theuser) AS result
WHERE  useridFB = 1234  

Or you might want to investigate rank depending on how you want ties to be treated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文