CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Help with SQL

  1. #1

    Help with SQL

    I have a problem with an SQL query. I have a huge book on SQL, but I don't know what I'm attempting to do is called, therefore I don't know what to look up. I will explain it as simply as possible.

    Two Tables.

    Table1 would look like this:
    Code:
    PK   PersonnelID
         FirstName
         LastName
    FK   MailingAddressID
    FK   PhysicalAddressID

    Table2 would look like this:


    Code:
    PK    AddressID
          AddressLine1
          AddressLine2
          City
          State
          ZIP
    I want to write a query to return the following in the result set:

    Code:
    FirstName, LastName, MailAddressLine1, MailAddressLine2, MailCity, MailState, MailZIP, PhysicalAddressLine1, PhysicalAddressLine2, PhysicalCity, PhysicalState, PhysicalZIP
    All of these fields would be in every returned record, whether a Physical Address or Mailing Address existed or not for each person.

    Can anyone help me out be either posting a relevent example, or telling me what I should look for in my SQL book.

    Thank you.

    David

  2. #2
    Join Date
    Dec 2009
    Posts
    596

    Re: Help with SQL

    What you want to look up in your book is the subject of JOINS. I'll take a shot at it in a lil bit when things settle down here in case you haven't figured it out by then.

  3. #3

    Re: Help with SQL

    Viperbyte,

    I am familiar with joins. But I think this is something more than that. The table that holds the addresses would hold two addresses for each person, so I need to take the two rows for each person and combine them on one row along with the information from the first table.

    It would be a simple join if there was only one address ID to link.

    David

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Help with SQL

    What and how did you try and what does not work?
    Victor Nijegorodov

  5. #5
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Help with SQL

    You need to include two instances of Table2 in your query (use aliases).

  6. #6

    Re: Help with SQL

    Figured it out. It's a subquery in the select statement.

    VictorN,

    Thanks for your help too, but the problem was that I didn't know where to start, or what it was called that I needed to do, therefore I COULDN'T look it up.

    I found the following on the internet, and I just need to change the table and field names and this should do exactly what I'm trying to do.

    Code:
    select p.product_name, p.supplier_name, (select order_id from order_items where product_id = 101) as order_id from product p where p.product_id = 101

  7. #7
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Help with SQL

    You have to get more familiar with joins. I already told you what you need is to reference Table2 twice in your query using aliases. Something like this:
    Code:
    SELECT t1.FirstName, t1.LastName,
        m.AddressLine1 AS MailAddressLine1,
        m.AddressLine2 AS MailAddressLine2,
        m.City AS MailCity,
        m.State AS MailState,
        m.ZIP AS MailZIP,
        p.AddressLine1 AS PhysicalAddressLine1,
        p.AddressLine2 AS PhysicalAddressLine2,
        p.City AS PhysicalCity,
        p.State AS PhysicalState,
        p.ZIP AS PhysicalZIP
    FROM Table1 t1, Table2 m, Table2 p
    WHERE t1.MailingAddressID = m.AddressID
    AND t1.PhysicalAddressID = p.AddressID

  8. #8
    Join Date
    Dec 2009
    Posts
    596

    Re: Help with SQL

    Hey DHillard; I see you have a solution. Isn't troubleshooting a blast??

  9. #9

    Re: Help with SQL

    You have the best solution jcaccia

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured