简体   繁体   English

PHP MYSQL查询结果“RANKING”

[英]PHP MYSQL query result “RANKING”

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: 我需要获得一个用户列表按点排序,并从我的命令行(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

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". 问题是我需要在AMFPHP上运行它并且我在测试PHP文件中测试它并且似乎我不能在相同的“mysql_query”中使用SET和SELECT。

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? 我看了,有些人习惯使用mysql_query来做这件事(我已经测试了它并且它有效),但是我能相信这是有效且无错吗? Does it work like in MySQL transactions or setting the @rank in a seperated query may cause unexpected results? 它是否像在MySQL事务中一样工作或在单独的查询中设置@rank可能会导致意外的结果?

Use this query without SET: 使用此查询而不使用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

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. 我选择尝试第一个选项,并使用内部联接在选择中设置@rank来构建查询。

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: 我不是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

I had to add the "AS" so that it didn´t throw an error for not having the alias on every derived table.... 我不得不添加“AS”,以便它不会因为没有在每个派生表上使用别名而引发错误....

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. PHP中的MySQL不允许在单个mysql_query()调用调用中执行多个查询,这是针对SQL注入攻击的一种安全措施。 That stops the Little Bobby Tables attack dead in its tracks (but doesn't protect against other types of injection attacks). 这可以阻止Little Bobby Tables攻击死亡(但不能防止其他类型的注入攻击)。

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

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. 但是,如果您通过AMF进行​​此操作,并且具有单独的AMF服务功能分别进行SET和SELECT,那么最有可能的是它将不起作用。 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. 每个AMF服务调用都是一个单独的HTTP请求,这意味着每次都有一个新的(和不同的)MySQL句柄,因此初始SET将在一个会话中完成,忘记,然后SELECT将在另一个会话中执行并具有完全不同的@秩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM