max(length(field)) in mysql

前端 未结 9 605
南方客
南方客 2021-01-30 06:25

If I say:

select max(length(Name)) 
  from my_table

I get the result as 18, but I want the concerned data also. So if I say:

s         


        
9条回答
  •  感情败类
    2021-01-30 07:01

    Use:

      SELECT mt.name 
        FROM MY_TABLE mt
    GROUP BY mt.name
      HAVING MAX(LENGTH(mt.name)) = 18
    

    ...assuming you know the length beforehand. If you don't, use:

      SELECT mt.name 
        FROM MY_TABLE mt
        JOIN (SELECT MAX(LENGTH(x.name) AS max_length
                FROM MY_TABLE x) y ON y.max_length = LENGTH(mt.name)
    

提交回复
热议问题