How do I select the longest 'string' from a table when grouping

后端 未结 5 2017
北海茫月
北海茫月 2020-12-23 15:45

Example:

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

WHERE [something]

GROUP BY partnumber, manufacturer, cond         


        
相关标签:
5条回答
  • 2020-12-23 16:13

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

    0 讨论(0)
  • 2020-12-23 16:15

    It seems i answered my own question, MAX(description) seems to work just fine.

    0 讨论(0)
  • 2020-12-23 16:16
    ORDER BY LENGTH(description) DESC LIMIT 1
    

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

    0 讨论(0)
  • 2020-12-23 16:29

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

    0 讨论(0)
  • 2020-12-23 16:34
    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`
    
    0 讨论(0)
提交回复
热议问题