Single table relational select

Discussion in 'MySQL' started by jwzumwalt, Oct 10, 2013.

  1. #1
        id  manager        Phone        contact
        1    John          ext1        Mark
        2    Mark          ext2        Sally
        3    David          ext1        John
        4    Sally          ext4        Rose
        5    Stephanie      ext5        Rose
        6    Rose          ext2        David
    Code (markup):
    I am trying to extract data from our large flat customer database that has lots of extra information. I have only done simple select and joins on two tables. I think this needs a join on itself but I am not sure.

    But the above table simplifies the data I am trying to pull.

    What select statement would I get each first name and the phone ext for the contact person?

    For example John needs the extension for Mark. So it would be
    John Ext2

    Thanks - Jan Zumwalt
     
    Last edited: Oct 10, 2013
    jwzumwalt, Oct 10, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #2
    an old school query would be:

    select t1.manager, t2.phone
    from mytable as t1, mytable as t2
    where t1.id = t2.contact_id

    assuming you can add a contact_id column which would be much safer than using the name... what if there were more than one person with a name?
     
    sarahk, Oct 10, 2013 IP