ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 okay, i will give an example at the start:I have 3 applesYou have only 1 appleI want to display the name which has the most apple, whos name is ted$sql = "SELECT * FROM appletable WHERE name = 'Ted'";$result = mysql_query($sql);$applesofme = mysql_num_rows($result);echo $appleofted;This would give the amount of rows that belong to Ted...$sql = "SELECT * FROM appletable WHERE name = 'You'";$result = mysql_query($sql);$applesofyou = mysql_num_rows($result);This would give the amount of rows that belong to you...but I wish to display the name with more appleapple is the number of rows by the wayand Name is just one of the column in mysql.ThanksTed Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/ Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Author Share Posted December 25, 2006 please refresh the page to see the editing done Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147676 Share on other sites More sharing options...
AndyB Posted December 25, 2006 Share Posted December 25, 2006 I've read that three times and I still can't figure out what it is you want to know ..... name, ted, apple, rows, or what??? Perhaps we need a more realistic example including sample data, and fewer imaginary variable names? Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147678 Share on other sites More sharing options...
chiprivers Posted December 25, 2006 Share Posted December 25, 2006 [quote author=AndyB link=topic=119900.msg491466#msg491466 date=1167088408]I've read that three times and I still can't figure out what it is you want to know ..... name, ted, apple, rows, or what??? Perhaps we need a more realistic example including sample data, and fewer imaginary variable names?[/quote]I think what he wants is something likeSELECT * FROM appletable GROUP BY namebut then with an extra value returned with each row to show how many records for each name! Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147679 Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Share Posted December 25, 2006 Let me jump in, since Ted is helping me out with this problem.I have a table named "pirep" and the first 5 fields on this table are:IDPirep | CreatedOn | IDPilot | PilotName | AircraftTitleAn example for rows would be:20061225 | 2006-12-25 13:08:01 | 1 | John Doe | American Airlines B767-300ER20061225 | 2006-12-25 13:08:01 | 3 | Efrain Ruiz | American Airlines B767-300ER20061225 | 2006-12-25 13:08:01 | 3 | Efrain Ruiz | American Airlines B767-300ER20061225 | 2006-12-25 13:08:01 | 2 | Jane Doe | American Airlines B767-300ER20061225 | 2006-12-25 13:08:01 | 4 | Tim Smith | American Airlines B767-300ERWhat I need to know, is how to make a query which will add all rows for EACH unique IDPilot value and then display the PilotName with the most rows.Using the information above, the pilotname that should be reported as having the most flights is Efrain Ruiz, since I have 2 rows. Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147681 Share on other sites More sharing options...
Barand Posted December 25, 2006 Share Posted December 25, 2006 SELECT pilotname, COUNT(*) as rowcountFROM pirepGROUP BY pilotnameORDER BY rowcount DESCLIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147695 Share on other sites More sharing options...
ERuiz Posted December 26, 2006 Share Posted December 26, 2006 That did the trick! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147704 Share on other sites More sharing options...
ted_chou12 Posted December 26, 2006 Author Share Posted December 26, 2006 what is LIMIT, i havent get to it before, could you explain please? Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147705 Share on other sites More sharing options...
chiprivers Posted December 26, 2006 Share Posted December 26, 2006 [quote author=ted_chou12 link=topic=119900.msg491493#msg491493 date=1167092477]what is LIMIT, i havent get to it before, could you explain please?[/quote]LIMIT sets the number of records returned, in this case it was only one record so by using the ORDER DESC syntax also, it returned just the first record, the largest. Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147707 Share on other sites More sharing options...
ted_chou12 Posted December 26, 2006 Author Share Posted December 26, 2006 oh, so if we have LIMIT 2, than it will give two results instead of 1, but two greatest? Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147709 Share on other sites More sharing options...
Barand Posted December 26, 2006 Share Posted December 26, 2006 [quote="mysql manual - you should read it sometime"] The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:SELECT * FROM tbl LIMIT 95,18446744073709551615;With one argument, the value specifies the number of rows to return from the beginning of the result set:SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rowsIn other words, LIMIT row_count is equivalent to LIMIT 0, row_count. [/quote] Quote Link to comment https://forums.phpfreaks.com/topic/31839-solved-display-the-greatest-number/#findComment-147710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.