Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > Get row that contains largest number

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-26-07, 17:31
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 108
Get row that contains largest number

Hello all,

I have several tables and i would like for each table to check a particular coulumn and return the row with the highest integer in the column that was checked.

So for one table, i have a column called biggest. How do i test which row has the biggest integer in that column? What would be the SQL statement? Would you use GREATEST() function??

This is what i have so far and it doesn't work:

PHP Code:
<?php

require_once("connect2database.php");

for(
$i=0$i<count($table); $i++){

$table $table[$i];

$query sprintf("SELECT subject, date FROM $table WHERE GREATEST(biggest) LIMIT 1"); //need to add ordering
$result = @mysql_query($query);

while(
$row mysql_fetch_array($result)){
echo 
$row['subject']."   ".$row['date'];
}
}
?>

I will appreciate any help.

Thanks all
Reply With Quote
  #2 (permalink)  
Old 09-27-07, 03:14
swapnilclarion swapnilclarion is offline
Registered User
 
Join Date: May 2007
Posts: 8
Hi,

Try using below query:

$query = sprintf("SELECT subject, date FROM $table ORDER BY biggest DESC LIMIT 1");

Thanks,
__________________
Swapnil Patil
DB Team @ Clarion
Clarion Technologies
(SEI CMMI Level 3)
Pune, India
www.clariontechnologies.co.in
www.vEmployee.com
Reply With Quote
  #3 (permalink)  
Old 09-27-07, 04:27
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,143
What's wrong with simply using
Code:
SELECT Max(biggest) FROM MyTable
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #4 (permalink)  
Old 09-27-07, 06:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
Quote:
Originally Posted by georgev
What's wrong with simply using...
because that doesn't return the entire row, as required
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #5 (permalink)  
Old 09-27-07, 07:16
georgev georgev is offline
SQL Apprentice
 
Join Date: Jan 2007
Location: hiding
Posts: 8,143
Ahh, touché... Yep, ignore my response then
__________________
George
You only stop learning when you stop asking questions.
Reply With Quote
  #6 (permalink)  
Old 09-27-07, 10:26
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
I'm guessing this "biggest" column is unique????
i.e. it can only contain one of each of the following 1,2,3,4,5,6......to infinity

So, your SQL could be
Code:
SELECT `subject` , `date` FROM <table> GROUP BY `biggest` HAVING biggest = (SELECT MAX(biggest) FROM <table>);

"i think"


note : and if it's not unique then how do you know you're getting the right row when you select the largest "biggest" value ?

Last edited by aschk : 09-27-07 at 10:39.
Reply With Quote
  #7 (permalink)  
Old 09-27-07, 13:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
Code:
SELECT subject, date FROM $table WHERE biggest = ( SELECT MAX(biggest) FROM $table )
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #8 (permalink)  
Old 09-27-07, 17:08
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 108
Quote:
Originally Posted by swapnilclarion
Hi,

Try using below query:

$query = sprintf("SELECT subject, date FROM $table ORDER BY biggest DESC LIMIT 1");

Thanks,

Your logic is one that i understand but some of the columns can be null (contain nothing) and that means it will always return one row from every table. Any way to modify it?
Reply With Quote
  #9 (permalink)  
Old 09-27-07, 17:12
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 108
Quote:
Originally Posted by r937
Code:
SELECT subject, date FROM $table WHERE biggest = ( SELECT MAX(biggest) FROM $table )

This sort of works but it is returning some rows that have equal values in the biggest columns. For example,two rows have a value of 0 and it returns both. Shall I add a limit. like this:

Code:
SELECT subject, date FROM $table FROM $table WHERE biggest = ( SELECT MAX(biggest) FROM $table ) LIMIT 1
Reply With Quote
  #10 (permalink)  
Old 09-27-07, 17:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
if two rows have the biggest value, then both should be returned

remember, you said you wanted the row, not the value

if there are two rows with the biggest value, then you cannot really pick just one of them
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #11 (permalink)  
Old 09-27-07, 17:54
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 108
Quote:
Originally Posted by r937
if two rows have the biggest value, then both should be returned

remember, you said you wanted the row, not the value

if there are two rows with the biggest value, then you cannot really pick just one of them

I also said "the row with the highest integer" - meaning one row should be returned. - So would the "LIMIT 1" work. I tried it and it seems to be working?
Reply With Quote
  #12 (permalink)  
Old 09-27-07, 18:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
yes, it "works" but it is not correct in my opinion

let's say that you are taking an SQL course, and on the final exam, the teacher says there will be a prize for the best mark, i.e. the person with the highest mark... and let's say both you and Suzy get the highest mark, and the teacher gives the prize to Suzy... and you're okay with that?????
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #13 (permalink)  
Old 09-27-07, 18:18
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 108
Quote:
Originally Posted by r937
yes, it "works" but it is not correct in my opinion

let's say that you are taking an SQL course, and on the final exam, the teacher says there will be a prize for the best mark, i.e. the person with the highest mark... and let's say both you and Suzy get the highest mark, and the teacher gives the prize to Suzy... and you're okay with that?????

I would force the teacher to buy another prize!

I see where your coming from but for my case i only need one result per table. Adding the result from the tables (7 tables) then i will have 7 results and not just one.
Reply With Quote
  #14 (permalink)  
Old 09-30-07, 23:53
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Fort Polk, LA
Posts: 500
Quote:
Originally Posted by compsci
but for my case i only need one result per table.

So add a tiebreaker by means of an ORDER BY clause. Otherwise your result is undefined. Do we really need to explain why undefined results are a bad thing?
Reply With Quote
  #15 (permalink)  
Old 10-01-07, 07:22
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 108
Quote:
Originally Posted by sco08y
Do we really need to explain why undefined results are a bad thing?

My result isn't undefined.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On