117

Example:

SELECT partnumber, manufacturer, condition, SUM(qty), AVG(price), description FROM parts

WHERE [something]

GROUP BY partnumber, manufacturer, condition

I have some descriptions that are blank, and there can be many partnumber, manufacturer, condition values, and on the group it seems to take the first description available, which can be blank. I'd like to get the longest description available.

I tried this:

MAX(LENGTH(description)) 

however, that returns the number of characters in the string. Is it possible to do what I'm trying to do in MySQL?

6 Answers 6

229

Try ORDER BY LENGTH(description) DESC and use LIMIT 1 to only get the largest.

0
31
ORDER BY LENGTH(description) DESC LIMIT 1

This will sort the results from longest to shortest and give the first result (longest.)

6
SELECT   partnumber, manufacturer, `condition`, SUM(qty), AVG(price), description
FROM     parts
WHERE    [something] AND LENGTH(description) = (
           SELECT MAX(LENGTH(description))
           FROM   parts AS p
           WHERE  p.partnumber   = parts.partnumber
              AND p.manufacturer = parts.manufacturer
              AND p.condition    = parts.condition
         )
GROUP BY partnumber, manufacturer, `condition`
3
  • This seems to me to 'expensive', in running time and written code, for the selection of one record. I don't think a sub-query is the best way to handle the problem.
    – MJH
    Nov 29, 2015 at 12:14
  • 1
    @rosa: you're right about verbosity, though I'm not sure performance time would be greatly different (both this and ORDER BY require filesort). Advantage of this version is that it returns all records of maximal length, not just a single indeterminate one.
    – eggyal
    Nov 29, 2015 at 12:39
  • 1
    @rosa: also, looking back over this question, I think my understanding was that the OP wanted to obtain the longest string in each group, rather than the longest result overall.
    – eggyal
    Nov 30, 2015 at 16:15
4

MAX(LENGTH(description)) returns length of longest value in Description column.

3

If a "description" contains multi-idiom characters, then you should use

MAX(CHAR_LENGTH(description))

For example :

SELECT LENGTH("Это тест, связанный с длиной строки в базе данных")

Resulting in 89.

Whereas :

SELECT CHAR_LENGTH("Это тест, связанный с длиной строки в базе данных")

Resulting in 49.

0

I found a solution. MAX(description) seems to work just fine.

1
  • 8
    That won't give you the longest description, rather the description which is the lexical maximum (i.e. in an alphabetical ordering). However, since that ordering will place any non-empty description after empty ones, it will always result in a non-empty description if one exists: perhaps this is sufficient for your needs?
    – eggyal
    Jun 4, 2012 at 14:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.