Closed Thread
Results 1 to 5 of 5

Thread: SQL result set containing results from 2 differnet rows

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    SQL result set containing results from 2 differnet rows

    I have a database table with four fields:

    ref_id
    ref_refid
    ref_amount
    ref_date

    The two id numbers is what I'm playing around with. These two numbers link to a customers table to find out who is making a referral and who was referred. What I want to do is select the phone numbers for both of these customers which are stored in these fields:

    homeArea
    homePrefix
    homeLine

    However, the problem I am running into is I can only select each column once when matching custId in the customers table with one of the ids. Is what I am wanting to do even possible or would I have to use two separate queries to do it? I can't use a join because it can't match the cust id twice on a row. I'm having a hard time describing my problem for a title though.

    Now say ref_id is 13 and ref_refid is 15.

    The result set should be:

    refPhone | custPhone
    555-555-5555 | 323-456-3233

    Any thoughts?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: SQL result set containing results from 2 differnet rows

    untested, but I believe you could join twice...
    something like this maybe?

    Code:
    select a.id, a.phone, b.id, b.phone from ref r join customer a on ref.id=a.id join customer b on ref.refid=b.id
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: SQL result set containing results from 2 differnet rows

    I think your right to use two joins. I think I got it.

    Thanks orjan. That is kind of confusing though. Thanks a bunch!

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: SQL result set containing results from 2 differnet rows

    Actually I just thought of something (stupid me over complicating things again) if customer x refers some customer y, and I want to display all the referrals that customer x has made, I don't need to select the information for customer x for every referral. It is always going to be the same, so I don't even need to do that.

    So I query the database once for the customer making the referrals phone number and then query for the appropriate phone numbers for the customers that have been referred.

    The only reason my query was confusing is because it is confusing. I was trying to do something that absolutely makes no sense.

  6. #5
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: SQL result set containing results from 2 differnet rows

    It could make sense without a problem to me, if you might wanna do a list so you order by or group by a either id or refid.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. XML Result from PHP?
    By Ricardo-san in forum PHP Development
    Replies: 14
    Last Post: 04-09-2009, 09:03 PM
  2. mySQL Rows in PHP
    By Affix in forum PHP Tutorials
    Replies: 0
    Last Post: 02-16-2009, 11:34 PM
  3. Copying Rows
    By Void in forum Database & Database Programming
    Replies: 6
    Last Post: 10-10-2007, 11:29 AM
  4. Getting Results
    By Lop in forum PHP Development
    Replies: 3
    Last Post: 01-10-2007, 10:16 AM

Tags for this Thread

Bookmarks

Posting Permissions

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