| |
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.
|
 |
|

09-26-07, 17:31
|
|
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
|
|

09-27-07, 03:14
|
|
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,
|
|

09-27-07, 04:27
|
|
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.
|
|

09-27-07, 06:24
|
|
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
|
|

09-27-07, 07:16
|
|
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.
|
|

09-27-07, 10:26
|
|
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.
|

09-27-07, 13:29
|
|
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 )
|
|

09-27-07, 17:08
|
|
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?
|
|

09-27-07, 17:12
|
|
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
|
|

09-27-07, 17:47
|
|
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
|
|

09-27-07, 17:54
|
|
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?
|
|

09-27-07, 18:00
|
|
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?????
|
|

09-27-07, 18:18
|
|
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.
|
|

09-30-07, 23:53
|
|
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?
|
|

10-01-07, 07:22
|
|
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. 
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|