问题
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?
回答1:
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
回答2:
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....
回答3:
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.
来源:https://stackoverflow.com/questions/4567871/php-mysql-query-result-ranking