Raised This Month: $51 Target: $400
 12% 

Question about SQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Yoav
Junior Member
Join Date: May 2010
Old 06-05-2010 , 05:04   Question about SQL
Reply With Quote #1

Hello, i'm trying to create an ingame ranking for my server in TF2.
My DB looks like this:

(table: player_kills)
auth ------------- value
STEAM:2313213 -- 35

now i'm on OnClientAuthorized and im using this query:

SELECT value FROM player_kills WHERE auth="STEAM:2313213"

how can i get the ranking of the player by its kills?
Yoav is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 06-05-2010 , 06:50   Re: Question about SQL
Reply With Quote #2

You could read all the values of all players, sort them by value, get the top # and list them.
Monkeys is offline
Yoav
Junior Member
Join Date: May 2010
Old 06-05-2010 , 06:52   Re: Question about SQL
Reply With Quote #3

Would you show me an example please?
Yoav is offline
Scone
Senior Member
Join Date: Apr 2010
Location: England
Old 06-05-2010 , 08:09   Re: Question about SQL
Reply With Quote #4

To find the rank of a player:

Code:
SET @rownum := 0;
SELECT rank FROM (SELECT @rownum := @rownum+1 AS rank, auth FROM player_kills ORDER BY value DESC) AS ranks WHERE auth = "STEAM_0:0:XXX";
Although this is slow and messy. It may be better to periodically calculate all players' ranks and store them in a separate table, so they can be looked up quickly.
__________________
Scone is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 06-08-2010 , 23:09   Re: Question about SQL
Reply With Quote #5

Wouldn't it be easier to just
Code:
SELECT TOP 10 value, auth
FROM player_kills
ORDER BY value ASC
Or is my SQL that rusty?

This returns the top 10 values and authentications to match.
(Probably need to add the ' wherever it's needed, though. They seem to fuck up wherever I put them)

Last edited by Monkeys; 06-08-2010 at 23:12.
Monkeys is offline
sfPlayer
Senior Member
Join Date: Dec 2007
Location: Germany
Old 06-09-2010 , 00:00   Re: Question about SQL
Reply With Quote #6

Something like
Code:
select count(*) as rank
from player_kills
where value > (select value from player_kills where auth = '..steamid..')
rank +1 is the actual rank you are looking for. It's just counting how many players have more kills.

Make sure value is an index.

Last edited by sfPlayer; 06-09-2010 at 00:04.
sfPlayer is offline
Scone
Senior Member
Join Date: Apr 2010
Location: England
Old 06-09-2010 , 04:30   Re: Question about SQL
Reply With Quote #7

Quote:
Originally Posted by sfPlayer View Post
Code:
select count(*) as rank
from player_kills
where value > (select value from player_kills where auth = '..steamid..')
Can't believe I didn't think of that! Much nicer than my solution
__________________
Scone is offline
NUdH3
Member
Join Date: Jul 2011
Old 11-26-2012 , 02:03   Re: Question about SQL
Reply With Quote #8

I use the same query.

With phpmyadmin it works, but in SourceMod i get a Syntax Error.

Code:
Format(query, sizeof(query), "SET @rownum := 0; SELECT rank FROM (SELECT @rownum := @rownum+1 AS rank, auth FROM player_kills ORDER BY value DESC) AS ranks WHERE auth = '%s'", pauth[8]);

I think thats not possible because it is a SQL_TQuery. I tested:

Code:
SQL_FastQuery(hDatabase, "SET @rownum := 0");
Format(query, sizeof(query), "SELECT rank FROM (SELECT @rownum := @rownum+1 AS rank, auth FROM player_kills ORDER BY value DESC) AS ranks WHERE auth = '%s'", pauth[8]);
SQL_TQuery...
that works, better solutions?

Last edited by NUdH3; 11-26-2012 at 02:03.
NUdH3 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:33.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode