Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 924
Lizard Avatar asked Jul 23 '10 14:07

Lizard


People also ask

How do I find the longest string in a column?

Find longest string in column. To find the longest string (name, word, etc.) in a column, you can use an array formula based on INDEX and MATCH, together with LEN and MAX. In the example shown, the formula in F6 is: Where "names" is the named range C5:C14. Note: this is an array formula and must be entered with control + shift + enter.

How to find the max length in a column in Excel?

Find the max length in a column. Here is a formula that can quickly find the max length in a column. Select a blank cell which will output the result, type this formula =MAX (LEN (A1:A63)) (A1:A63 is the range you use if you want to use a column, change it to A:A), and press Shift+ Ctrl + Enter keys together. See screenshot:

How to find the longest string in a range with criteria?

To find the longest string in a range with criteria, you can use an array formula based on INDEX, MATCH, LEN and MAX. In the example shown, the formula in F6 is: { = INDEX ( names , MATCH ( MAX ( LEN ( names ) * ( class = F5 )), LEN ( names ) * (...

How to find the shortest and longest Firstname in MySQL?

Now let’s find the shortest and longest firstName from the table we have just created using char_length (), min (), and max () functions and LIMIT clause. Use the ow syntax to find the shortest firstName in the friends table:


2 Answers

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
like image 79
fabrik Avatar answered Nov 01 '22 18:11

fabrik


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); 
?>
like image 40
Lizard Avatar answered Nov 01 '22 19:11

Lizard