PHP MYSQL 查询结果“RANKING”

发布于 2024-10-09 23:48:38 字数 671 浏览 0 评论 0原文

我需要获取按点排名的用户列表,并且从我的命令行(MySQL)能够生成必要的代码:

SET @rank=0;
SELECT rank, iduser, pontos FROM (
SELECT @rank:=@rank+1 AS rank,
       SUM(points.points) AS pontos,
       points.iduser,
       users.name,
       users.idade
  FROM points
       INNER JOIN
       users
       ON (points.iduser = users.id)
 WHERE (users.idade >= %s) AND (users.idade <= %s)
GROUP BY points.iduser ORDER BY pontos DESC) AS totals WHERE iduser = %s

问题是我需要它在 AMFPHP 上运行,并且我已经在测试 PHP 文件中测试了它似乎我无法在同一个“mysql_query”中使用 SET 和 SELECT。

我已经看过并且有些人习惯使用 mysql_query 来执行此操作(我已经测试过它并且它有效),但是我可以相信它是有效且无错误的吗?它是否像 MySQL 事务中一样工作,或者在单独的查询中设置 @rank 可能会导致意外结果?

I need to get a list of users Ranking by points and from my command line (MySQL) is was able to generate the necessary code:

SET @rank=0;
SELECT rank, iduser, pontos FROM (
SELECT @rank:=@rank+1 AS rank,
       SUM(points.points) AS pontos,
       points.iduser,
       users.name,
       users.idade
  FROM points
       INNER JOIN
       users
       ON (points.iduser = users.id)
 WHERE (users.idade >= %s) AND (users.idade <= %s)
GROUP BY points.iduser ORDER BY pontos DESC) AS totals WHERE iduser = %s

The problem is that I need this to run on AMFPHP and I´ve tested it in a test PHP file and seems that I can´t use the SET and SELECT in the same "mysql_query".

I´ve looked and some used to mysql_query to do this (I´ve tested it and it works), but can I trust this to be effective and error free? Does it work like in MySQL transactions or setting the @rank in a seperated query may cause unexpected results?

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

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

发布评论

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

评论(3

绿光 2024-10-16 23:48:38

使用此查询而不使用 SET:

SELECT rank, iduser, pontos FROM (
SELECT @rank:=@rank+1 AS rank,
       SUM(points.points) AS pontos,
       points.iduser,
       users.name,
       users.idade
  FROM points
       INNER JOIN
       users
       ON (points.iduser = users.id)
             INNER JOIN
             (SELECT @rank :=0)
 WHERE (users.idade >= %s) AND (users.idade <= %s)
GROUP BY points.iduser ORDER BY pontos DESC) AS totals WHERE iduser = %s

Use this query without SET:

SELECT rank, iduser, pontos FROM (
SELECT @rank:=@rank+1 AS rank,
       SUM(points.points) AS pontos,
       points.iduser,
       users.name,
       users.idade
  FROM points
       INNER JOIN
       users
       ON (points.iduser = users.id)
             INNER JOIN
             (SELECT @rank :=0)
 WHERE (users.idade >= %s) AND (users.idade <= %s)
GROUP BY points.iduser ORDER BY pontos DESC) AS totals WHERE iduser = %s
红玫瑰 2024-10-16 23:48:38

感谢您的快速解答。我选择尝试第一个选项,并使用内部联接在选择中设置@rank 来构建查询。

我必须稍微改变一下,因为最终结果不是我所期望的,因为我在添加增量后按点对列表进行排序。我不是 MySQL 专家,但这就是我现在所做的工作:

SELECT rank, pontos FROM (
SELECT @rank:=@rank+1 AS rank, iduser, idade, pontos FROM (
 SELECT SUM(points.points) AS pontos,
 points.iduser,
        users.name,
        users.idade
        FROM points 
        INNER JOIN
        users
        ON (points.iduser = users.id)
 WHERE (users.idade >= 10) AND (users.idade <= 24)
 GROUP BY points.iduser ORDER BY pontos DESC ) AS pointsList
 INNER JOIN
 (SELECT @rank :=0) AS ranker ) AS ranking WHERE iduser = 2

我必须添加“AS”,这样它就不会因为每个派生表上没有别名而抛出错误......

Thanks for the quick answers. I opted to try the first option and build the query with the inner join setting the @rank in the select.

I had to change a litle because the end result wasn´t what I expected as I was ordering the list by the points after adding the incremental. I´m not an expert in MySQL but this is what I made that for now worked:

SELECT rank, pontos FROM (
SELECT @rank:=@rank+1 AS rank, iduser, idade, pontos FROM (
 SELECT SUM(points.points) AS pontos,
 points.iduser,
        users.name,
        users.idade
        FROM points 
        INNER JOIN
        users
        ON (points.iduser = users.id)
 WHERE (users.idade >= 10) AND (users.idade <= 24)
 GROUP BY points.iduser ORDER BY pontos DESC ) AS pointsList
 INNER JOIN
 (SELECT @rank :=0) AS ranker ) AS ranking WHERE iduser = 2

I had to add the "AS" so that it didn´t throw an error for not having the alias on every derived table....

口干舌燥 2024-10-16 23:48:38

PHP 中的 MySQL 不允许在单个 mysql_query() 调用调用中执行多个查询,作为针对 SQL 注入攻击的安全措施。这可以阻止 Little Bobby Tables 攻击(但不能防止其他类型的注入攻击) 。

只要您使用与查询相同的脚本会话和相同的数据库句柄执行 SET 查询,就没有理由不能将其拆分为两个单独的调用。

mysql_query("SET @rank:=0;");
$res = mysql_query("SELECT ....");

会工作得很好。但是,如果您通过 AMF 执行此操作,并且具有单独的 AMF 服务函数来分别执行 SET 和 SELECT,那么它很可能无法工作。每个 AMF 服务调用都是一个单独的 HTTP 请求,这意味着每次都有一个新的(且不同的)MySQL 句柄,因此初始 SET 将在一个会话中完成(忘记了),然后 SELECT 将在另一个会话中执行并具有完全不同的 @秩。

MySQL in PHP does not allow multiple queries to be executed in a single mysql_query() call invocation as a security measure against SQL injection attacks. That stops the Little Bobby Tables attack dead in its tracks (but doesn't protect against other types of injection attacks).

As long as you do the SET query using the same script session and same database handle as the query, there's no reason you can't split that into two seperate calls.

mysql_query("SET @rank:=0;");
$res = mysql_query("SELECT ....");

would work fine. However, if you're doing this via AMF and have seperate AMF service functions to do the SET and SELECT seperately then most likey it won't work. Each AMF service call is a seperate HTTP request, which means a new (and different) MySQL handle each time, so the initial SET would be done in one session, forgotten, then the SELECT will execute in another session and have a completely different @rank.

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