Forum : How do I find the longest value in my mysql table
Brief description  about Online courses   join in Online courses
View  's Profile

How do I find the longest value in my mysql table

sir,

Can any one tell me how to find the longest value in my mysql table
Asked by | Nov 27, 2009 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Naveen,

Often what you need to do is work out which function will get you closest to what you want to achieve, and then combine this with your knowledge of manipulating queries to get exactly what you want.

So you probably know that the LENGTH() function applied to a fieldname will return the length of that field.

So then all you need is the relevant query to find the length of the longest item in a field like so:

<?php
$q = mysql_query("SELECT LENGTH(firstname) AS thelength FROM myfriends ORDER BY thelength DESC LIMIT 1");
$longestname = mysql_result($q,0,0);

echo "The longest first name is $longestname letters long!";
?>
Nov 27, 2009