Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Get username with id out other table

    The idea is pretty simple but I can't get it working...

    I have two tables:

    Persons

    Id - Username
    1 - Test
    2 - Test2


    Orders

    O_Id - P_Id
    2000 - 1
    3234 - 2
    4836 - 1
    5512 - 1


    I want this as output


    O_Id - Username
    2000 - Test
    3234 - Test2
    4836 - Test
    5512 - Test

    Could anyone help me please?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    This is the most basic of all joins. You can either use an impicit join, thus:
    Code:
    SELECT O.o_id, P.username
    FROM Orders AS O, Persons AS P
    WHERE O.p_id = P.id
    ORDER BY O.o_id
    Or you can use an explicit INNER JOIN, thus:
    Code:
    SELECT O.o_id, P.username
    FROM Orders AS O INNER JOIN Persons AS P
    ON O.p_id = P.id
    ORDER BY O.o_id
    This should be about lesson 3 or 4 in any SQL book or class.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •