how to make a query that hits 2 or more tables at same time?

Discussion in 'Web Design and Development' started by CavemanUK, Feb 17, 2011.

  1. CavemanUK macrumors 6502

    CavemanUK

    Joined:
    Jun 29, 2006
    Location:
    Rhyl, North Wales
    #1
    Hi, lets say i have 3 tables set out like this....

    tbl_Customers
    -------------
    id
    CustomerName
    customertype_id
    customerreferrer_id

    tbl_CustomerTypes (eg.. Private, Contract etc)
    ------------------
    id
    Type

    tbl_CustomerReferrers (eg.. Word of Mouth, Advertising etc)
    ---------------------
    id
    Referrers


    How would i send a query that pulls an array with the following fields..

    CustomerName CustomerType CustomerReferrer
    Jones Contract Newspaper Advert
    Smith Private Word of Mouth

    etc

    hope this makes sense.
     
  2. sleaver macrumors member

    Joined:
    Jul 21, 2010
    #3
    As has been said, have a look at JOINS.

    Although this could be putting my SQL skills at risk I thought I'd try and post my first helpful post rather than asking questions, the following should do what you want:

    PHP:
    SELECT CustomerNameType AS CustomerTypeReferrer AS CustomerReferrer
    FROM tbl_Customers
    LEFT JOIN tbl_CustomerTypes ON tbl_Customers
    .customertype_id tbl_CustomerTypes.id
    LEFT JOIN tbl_CustomerReferrers ON tbl_Customers
    .customerreferrer_id tbl_CustomerReferrers.id
     
  3. Laird Knox macrumors 68000

    Joined:
    Jun 18, 2010
    #4
    Unless you are working with Google AppEngine - in that case you are just out of luck. ;)
     

Share This Page