Search code examples
mysqlstring-length

Finding field with longest length in a column


How do I find the field with the longest length of a specific column in a MySQL table?


Solution

  • 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); 
    ?>