max(length(field)) in mysql

前端 未结 9 575
南方客
南方客 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 06:35

    In case you need both max and min from same table:

        select * from (
    (select city, length(city) as maxlen from station
    order by maxlen desc limit 1)
    union
    (select city, length(city) as minlen from station
    order by minlen,city limit 1))a;
    
    0 讨论(0)
  • 2021-01-30 06:37
    SELECT  name, LENGTH(name) AS mlen
    FROM    mytable
    ORDER BY
            mlen DESC
    LIMIT 1
    
    0 讨论(0)
  • 2021-01-30 06:38

    Use CHAR_LENGTH() instead-of LENGTH() as suggested in: MySQL - length() vs char_length()

    SELECT name, CHAR_LENGTH(name) AS mlen FROM mytable ORDER BY mlen DESC LIMIT 1

    0 讨论(0)
  • 2021-01-30 06:47

    Edited, will work for unknown max() values:

    select name, length( name )
    from my_table
    where length( name ) = ( select max( length( name ) ) from my_table );
    
    0 讨论(0)
  • 2021-01-30 06:47
    Select URColumnName From URTableName Where length(URColumnName ) IN 
    (Select max(length(URColumnName)) From URTableName);
    

    This will give you the records in that particular column which has the maximum length.

    0 讨论(0)
  • 2021-01-30 06:54

    I suppose you could use a solution such as this one :

    select name, length(name)
    from users
    where id = (
        select id
        from users
        order by length(name) desc
        limit 1
    );
    

    Might not be the optimal solution, though... But seems to work.

    0 讨论(0)
提交回复
热议问题