How Do You Find The Highest Value In MySQL Database?

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
TNW
Registered User
Posts: 53
Joined: Sat Aug 27, 2005 11:37 pm
Location: IN
Contact:

How Do You Find The Highest Value In MySQL Database?

Post by TNW »

Hello I need to know how to find a highest value in a row and a certain collumn how do I identify this value with a special attribute like a diferent background color out of a row of numbers?
Im writing my signature here for what reason i do not understand. No i wont put my name here.
Montana
Registered User
Posts: 93
Joined: Sun Jun 20, 2004 7:43 pm
Contact:

Post by Montana »

i dont know if this is what you mean or if this work for you..

Code: Select all

$sql = "SELECT *
	FROM " . TABLE . "
	ORDER BY ROW DESC";

you can change DESC with ASC
Sorry for my bad english :?
montanas-corner
agent00shoe
Registered User
Posts: 373
Joined: Sat Feb 12, 2005 9:39 pm

Post by agent00shoe »

Montana wrote: i dont know if this is what you mean or if this work for you..

Code: Select all

$sql = "SELECT *
	FROM " . TABLE . "
	ORDER BY ROW DESC LIMIT 1";

you can change DESC with ASC


Edited to add LIMIT 1. That would just give you the highest 1 without listing all rows in descending order.
TNW
Registered User
Posts: 53
Joined: Sat Aug 27, 2005 11:37 pm
Location: IN
Contact:

Post by TNW »

:? me do not comprehend (understand) please explain how Id use that ina variable wich has allready stored an entire row from a database ive tried

Code: Select all

if ($row['hits'] == max($row['hits']) )
  {
    do special backgroudn color for the table wich is suposed to be a higlisght to point out highest hits
   }
 else
  {
   regular color nothing highlighted and siplay my error its not working right
   }
Im writing my signature here for what reason i do not understand. No i wont put my name here.
Montana
Registered User
Posts: 93
Joined: Sun Jun 20, 2004 7:43 pm
Contact:

Post by Montana »

maby a stupid question...

but why would you highlight something when you can sort it out bij highest number ?

example:

website hits uhits
www.... 5 13
www... 4 12
etc etc

you can use this code:
$sql = "SELECT *
FROM " . SOME_TABLE . "
ORDER BY uhits DESC";
$row stuff here
Sorry for my bad english :?
montanas-corner
TNW
Registered User
Posts: 53
Joined: Sat Aug 27, 2005 11:37 pm
Location: IN
Contact:

Post by TNW »

bc this is what I see

Date Hits Unique Hits

01/09/2005 0 0 Now I have the highest row
02/09/2005 0 0 highlighted now I need the
03/09/2005 0 0 lowest row in tips on how to do
04/09/2005 0 0 this part ???
05/09/2005 0 0 ------------------------------------
06/09/2005 0 0 The highlited row is to give fast
07/09/2005 0 0 look at wich is lowest highest
08/09/2005 0 0 and the highlighted for todays
09/09/2005 0 0 date for good visual aid.
10/09/2005 0 0
11/09/2005 0 0
12/09/2005 0 0
13/09/2005 0 0
14/09/2005 0 0
15/09/2005 0 0
16/09/2005 0 0
17/09/2005 0 0
18/09/2005 0 0
19/09/2005 0 0
20/09/2005 0 0
21/09/2005 0 0
22/09/2005 0 0
23/09/2005 0 0
24/09/2005 0 0
25/09/2005 6 1
26/09/2005 4 2
27/09/2005 0 0
28/09/2005 0 0
29/09/2005 0 0
30/09/2005 0 0
Im writing my signature here for what reason i do not understand. No i wont put my name here.
User avatar
D¡cky
Former Team Member
Posts: 11812
Joined: Tue Jan 25, 2005 8:38 pm
Location: New Hampshire, USA
Name: Richard Foote
Contact:

Post by D¡cky »

I think if you do another query to get the max value, then when you go through the rows, you can compare to the max value and set the row color when there is a match. Like this:

Code: Select all

$sql = "SELECT MAX(hits) AS max_hits FROM " . TABLE;
	if (!($result = $db->sql_query($sql)))
	{
       	message_die (GENERAL_ERROR, 'Unable to retrieve hit count');
	}
	$row = $db->sql_fetchrow($result); 
	$max_hits = $row['max_hits'];

You have now assigned the highest value of hits to $max_hits.

Now, you can do your regular SELECT to select all the rows from the table

Code: Select all

$sql = "SELECT * FROM " .TABLE . "blahblah";
As you are going through the rows, do your comparison

Code: Select all

if ( $row['hits'] == $max_hits )
  {
    do special backgroudn color for the table wich is suposed to be a higlisght to point out highest hits
   }
 else
  {
   regular color nothing highlighted and siplay my error its not working right
   }
Have you hugged someone today?
TNW
Registered User
Posts: 53
Joined: Sat Aug 27, 2005 11:37 pm
Location: IN
Contact:

Post by TNW »

Ill give it a try thx
Im writing my signature here for what reason i do not understand. No i wont put my name here.
Post Reply

Return to “[2.0.x] MOD Writers Discussion”