Jump to content


Photo

database functions: is there one for...


  • Please log in to reply
5 replies to this topic

#1 klaroen

klaroen
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 18 June 2006 - 07:59 PM

Oke this is the request:
Is there a query function or code to do this:

I have a database, and there are kind of diffrent number in it: 500 , 360 , 578, 9978, 7589, 125, 154, etc...

Now when I select one of those, can I ask with a code with place he has from the top?
So: I select the number 578 for example, can it tell me what 'rank' it has? so it should be 3th...

Is there a code for that?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 18 June 2006 - 08:02 PM

Yes. But we'll need a great deal more information to tell you how. What are those numbers? What is 'rank'. What's the database table structure?
Legend has it that reading the manual never killed anyone.
My site

#3 klaroen

klaroen
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 18 June 2006 - 08:16 PM

Imagine: I have a list of products and they all have their price.

<?php
   $player=$_SESSION['player'];
    $userstats="SELECT * from koc_products where username='$player'";
    $userstats2=mysql_query($userstats) or die("Error while connecting to database! Please report this to an admin!");
    $stat=mysql_fetch_array($userstats2);

   $ranking=[b]thecodeineed[/b]($stat[price]);

print(" The product is our [b]?[/b] most expansive product!");


So as you see the code I need should tell me how muchst he is in the list of most expensive products...


Is that understandable?

(ps: rank is the number of how high he is ( how muschest)(is that spelled like that?? )

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 19 June 2006 - 05:05 AM

if you are simply looking for the highest number in the list, you would do like this (assuming "price" is the column name) :
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']select[/span] price from tablename order by price desc limit 1
[!--sql2--][/div][!--sql3--]
so if you had a column price and it had the following info in it:

price
===
45
453
6432
583
5432
945
34984

that query would return 34984
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 klaroen

klaroen
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 19 June 2006 - 06:49 AM

yeah, that I know :P... I'll give another example:

In a online game every player has his troops and has his attack power and defence power. Now, if I select the defence from the player that is logged in with query can I get the rank of his defence with a function?
So his defence is like 5009 and there is only one player better with 6582 and a lot players are lower... Now, can I ask my database to count out for me that he's defence is the second highest?
Or for other players how high their defence is (counted in places)...


(get it?)

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 19 June 2006 - 08:09 AM

i don't know if mysql has a built in function for that or not, but i suppose you could write a script in php for that. and even still, there has GOT to be a better way than this, but this is my ghetto code:
$username = 'klaroen';

$sql = "select * from table order by defense desc";
$result = mysql_query($sql);
while ($info = mysql_fetch_array($result)) {
   $playerinfo[] = $info;
}
$numplayers = count($playerinfo);
$pos = 1;
while ($playerinfo[$pos]['playername'] != $username) {
   $pos++;
}
echo "Name: " . $username . "<br>";
echo "Defense: " . $playerinfo[$pos]['defense'] . "<br>";
echo "Defense rank: " . $pos;

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users