PHP MYSQL query result "RANKING"
Translations
Englishالعربية
български
català
中文
čeština
dansk
Nederlands
eesti
suomi
français
Deutsch
Ελληνικά
עברית
हिंदी
magyar
Bahasa Indonesia
italiano
日本語
한국어
latviešu
lietuvių
norsk
polski
Português
română
русский
slovenčina
slovenski
español
svenska
ไทย
Türkçe
українська
Tiếng Việt
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?
This question and answers originated from www.stackoverflow.com
Question by fkessler (12/31/2010 3:25:42 AM)
Answer |
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
Answer by Cybernate
Find More Answers