Page 1 of 1
Search the highest number(in a row) in a MySQL table...
Posted: Fri May 05, 2006 12:49 pm
by The-Master
hi, if i have a table like this:
--------------------------
table_name:
id - age(int)
--------------------------
and i have these records:
--------------------------
id - age
1 - 34
2 - 45
3 - 19
--------------------------
how can i make that my script will find the highest "age" in the db records and if there are no records it will define a the var "$age" as '1'.(in this DB the highest "age" is 45...)
i am not asking you to write me a code i just want to know the function(or resource) that does this action...
Posted: Fri May 05, 2006 1:32 pm
by Christopher
You can either use the MAX() function or "ORDER BY age DESC LIMIT 1"
I'm not sure which is faster, probably MAX().
Posted: Fri May 05, 2006 1:34 pm
by timvw
Just get yourself a (basic) book/tutorial/... on SQL...
(The limit clause isn't standard sql... Admitted, one could argue that there no such thing as a standard query language...)
Posted: Fri May 05, 2006 2:34 pm
by raghavan20
timvw wrote: Admitted, one could argue that there no such thing as a standard query language...)
wikipedia wrote:
1986 SQL-86 SQL-87 First published by ANSI. Ratified by ISO in 1987.
1989 SQL-89 Minor revision.
1992 SQL-92 SQL2 Major revision.
1999 SQL:1999 SQL3 Added regular expression matching, recursive queries, triggers, non-scalar types and some object-oriented features. (The last two are somewhat controversial and not yet widely supported.)
2003 SQL:2003 Introduced XML-related features, window functions, standardized sequences and columns with auto-generated values (including identity-columns).
(See Eisenberg et al.: SQL:2003 Has Been Published.)
Still the SQL is being maintained by ISO and ANSI. Eventhough there are deviations and extensions of standard SQL but I think still there are basics which are adhered.
Posted: Fri May 05, 2006 3:17 pm
by timvw
As soon as one tries to restrict the maximum number of rows in a resultset, quite common imho, one already experiences how much the standard is really worth if you take into consideration all the dialect implementations for this functionality...
(I admit, since SQL2003, it has become possible with 'the standard')
Posted: Fri May 05, 2006 7:18 pm
by The-Master
thanks for the help...