I’m trying to figure out how to get a specific users rank depending on the users average points, but with no luck…
Here is what I have tried:
$sql = mysql_query("SELECT COUNT(*) rank FROM ".$prefix."_users WHERE average_points > (SELECT points/played AS average_points FROM ".$prefix."_users WHERE id = $currplayer) AND played > 24") or die (mysql_error());
This can not be done due to the average_points is set within the second SELECT… What to do?
CREATE VIEW player_averages
AS
SELECT points/played AS average_points
FROM ".$prefix."_users
WHERE played > 24
;
SELECT COUNT(*) + 1 AS rank
FROM player_averages
WHERE average_points >
( SELECT average_points
FROM player_averages
WHERE id = $currplayer )
;
$sql = mysql_query("CREATE VIEW player_averages
AS
SELECT points/played AS average_points
FROM ".$prefix."_users
WHERE played > 24
;
SELECT COUNT(*) + 1 AS rank
FROM player_averages
WHERE average_points >
( SELECT average_points
FROM player_averages
WHERE id = $currplayer )
;") or die (mysql_error());
But got this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT COUNT(*) + 1 AS rank FROM player_averages WHERE average_points > ’ at line 7
This is a complicated solution and I can’t figure out where it does go wrong? Please help…
$sql = mysql_query("CREATE VIEW player_averages
AS
SELECT points/played AS average_points
FROM ".$prefix."_users
WHERE played > 24
");
$sql = mysql_query("SELECT COUNT(*) + 1 AS rank
FROM player_averages
WHERE average_points >
( SELECT average_points
FROM player_averages
WHERE id = $currplayer )
;") or die (mysql_error());
$row = mysql_fetch_assoc($sql);
Got this error: Unknown column ‘id’ in ‘where clause’
you only need to create the view once – in fact, you cannot create it more than once
so putting it into your php program seems pointless (to me), because you’re just going to have to bypass it on every execution other than the very first… ever
running it outside of php means through some front end utility program like the mysql browser, phpmyadmin, heidisql, or similar
aaaaack!!! my bad
the view actually needs the id as well…
CREATE VIEW player_averages
AS
SELECT id
, points/played AS average_points
FROM user_points
WHERE played > 24
CREATE
[COLOR="#FF0000"]OR
REPLACE[/COLOR] VIEW player_averages
AS
SELECT id
, points/played AS average_points
FROM user_points
WHERE played > 24
This is not to say that the code for view creation should be repeated every time the view is used by a query. It just saves an explicit DROP VIEW command.
it’s kind of like a code variable, in that when you write a query that references a view, mysql goes and grabs the SQL for the view, and stuffs it into your query where the view name is, and then proceeds to interpret/parse/execute your query with the view’s SQL where the view’s name is
in the case of your ranking query, we need to make two references to the view, and it’s more awkward to write a subquery in there where the view name is
however, it is possible to do it without the view –
SELECT COUNT(*) + 1 AS rank
FROM [COLOR="#0000FF"]( [COLOR="#FF0000"]SELECT id
, points/played AS average_points
FROM user_points
WHERE played > 24[/COLOR]
) AS player_averages_[B]1[/B][/COLOR]
WHERE average_points >
( SELECT average_points
FROM [COLOR="#0000FF"]( [COLOR="#FF0000"]SELECT id
, points/played AS average_points
FROM user_points
WHERE played > 24[/COLOR]
) AS player_averages_[B]2[/B][/COLOR]
WHERE id = $currplayer )