max(length(field)) in mysql

前端 未结 9 576
南方客
南方客 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:56
    select * 
    from my_table 
    where length( Name ) = ( 
          select max( length( Name ) ) 
          from my_table
          limit 1 
    );
    

    It this involves two table scans, and so might not be very fast !

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

    Ok, I am not sure what are you using(MySQL, SLQ Server, Oracle, MS Access..) But you can try the code below. It work in W3School example DB. Here try this:

    SELECT city, max(length(city)) FROM Customers;
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题