How do I find the longest value in my mysql table?
I have a php script and I want to search my table to find the record with the longest string?
Any ideas?
How do I find the longest value in my mysql table?
I have a php script and I want to search my table to find the record with the longest string?
Any ideas?
You can use the mysql command LENGTH()
<?php
$q = mysql_query("SELECT LENGTH(yourfield) AS fieldlength FROM yourtable ORDER BY fieldlength DESC LIMIT 1");
echo $longestfield = mysql_result($q,0);
?>
MySQL has a lot of string functions you can use:
SELECT LENGTH(col) as my_len FROM my_table ORDER BY my_len DESC LIMIT 1
More funky version (it works):
SELECT MAX(LENGTH(col)) FROM my_table
It wasn't your original question, but if you are seeking the maximum length to help you with table the definition, you should check out PROCEDURE ANALYSE():
http://dev.mysql.com/doc/refman/5.0/en/procedure-analyse.html