SELECT row with minimum value length?

Discussion in 'MySQL' started by thuankkk, Feb 20, 2009.

  1. #1
    Hi,

    I have table person, some persons have the same first name:

    firstname lastname
    John Lennon
    John Lewis
    John Chow
    John McCain
    ...

    I use this query
    SELECT lastname FROM person WHERE firstname='John';

    Now I wanna select only one rows, whose lastname has the min. value length (in this case, it's Chow - 4 characters)
    How can I do that?
     
    thuankkk, Feb 20, 2009 IP
  2. bl4ckwolf

    bl4ckwolf Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    i would try with select lastname, firstname, min(len(firstname)) from person group by lastname

    len is a function for lenght. you should have something similar with the DB you are using.
     
    bl4ckwolf, Feb 21, 2009 IP
  3. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #3
    Thanks for your help.

    But
    It looks like my MySQL doesn't have LEN function...
     
    thuankkk, Feb 22, 2009 IP
  4. bl4ckwolf

    bl4ckwolf Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    A simple research in Google with "MySql lenght" let me found this function :

    CHAR_LENGTH() Return number of characters in argument

    Remember! Google is your friend and if you want to succeed on programming, you will have sometimes to do yourself the research.

    Hope this help!
     
    bl4ckwolf, Feb 22, 2009 IP
    thuankkk likes this.
  5. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #5
    Thank you! It works :) Rep+ for your help :)
     
    thuankkk, Feb 26, 2009 IP