many-to-many query -- buyers and sellers

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default many-to-many query -- buyers and sellers

    Hi,

    I'm trying to do something that seems very simple, but has me stumped.
    The real application I'm working on is quite complex, so I'll just use
    a "buyers and sellers" analogy, which sort of summarizes what I'm
    really trying to do.

    Basically, I have 2 tables:
    Table 1: Users
    field 1: user_id
    field 2: username

    Table 2: Transactions
    field 1: transaction_id
    field 2: buyer_id
    field 3: seller_id

    So, what I'm trying to do is build a single query that will return the
    usernames of both the buyer and the seller.

    Your help is greatly appreciated.

    Ross
    Ross Guest

  2. Similar Questions and Discussions

    1. Top sellers SQL ?
      Hi - using asp/access/vbscritp Have a webshop where all orders are stored in a cart table in the database. Need an advice on how to get an SQL...
    2. New Website: www.bquote.com Sellers compete > Buyers get the best price!
      We worked very hard to make a website called http://www.bquote.com, and we went live on September 19, 2003 so if you would take a look at it we...
    3. O.T. digital camera sellers
      I hate to start another thread on this subject but I need some advice. I cannot find the camera I am seeking at B & H Photo ;( Has anyone ever dealt...
    4. need advice from seasoned B & H buyers...
      ] If your lenses for FE2 aren't AF lenses, then they will be the next best thing to worthless. The new crop of Nikon cameras won't even meter...
    5. FileMaker 6 rebate for New Zealand buyers
      This is from the latest Apple New Zealand email that's just arrived in my in-box: Save $125 on FileMaker Pro 6 ----------------------------...
  3. #2

    Default Re: many-to-many query -- buyers and sellers

    Ross wrote:
    > Hi,
    >
    > I'm trying to do something that seems very simple, but has me stumped.
    > The real application I'm working on is quite complex, so I'll just use
    > a "buyers and sellers" analogy, which sort of summarizes what I'm
    > really trying to do.
    >
    > Basically, I have 2 tables:
    > Table 1: Users
    > field 1: user_id
    > field 2: username
    >
    > Table 2: Transactions
    > field 1: transaction_id
    > field 2: buyer_id
    > field 3: seller_id
    >
    > So, what I'm trying to do is build a single query that will return the
    > usernames of both the buyer and the seller.
    >
    > Your help is greatly appreciated.
    >
    > Ross
    What database? I'm going to assume SQL Server. I will not be happy if you
    are using Access because that will change the syntax required in the query.
    The good news (for you) is that you will be able to use the Access Query
    Builder to do what I describe below.

    I assume buyer_id and seller_id both point to a record in the Users table.
    You need to use a table alias to join to the Users tables twice:

    Select b.surname as Buyer, s.surname as Seller
    FROM Transactions t
    INNER JOIN Users As b ON t.buyer_id = b.user_id
    INNER JOIN Users As s ON t.seller_id = s.user_id

    If using Access, use the Query Builder Design View and add in the Users
    table twice, creating the joins to the appropriate field in Transactions
    from each copy of the Users table.

    HTH,
    Bob Barrows



    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows Guest

  4. #3

    Default Re: many-to-many query -- buyers and sellers

    "Ross" <rolo@houston.rr.com> wrote in message
    news:99b34533.0311101103.1308495a@posting.google.c om...
    > Hi,
    >
    > I'm trying to do something that seems very simple, but has me stumped.
    > The real application I'm working on is quite complex, so I'll just use
    > a "buyers and sellers" analogy, which sort of summarizes what I'm
    > really trying to do.
    >
    > Basically, I have 2 tables:
    > Table 1: Users
    > field 1: user_id
    > field 2: username
    >
    > Table 2: Transactions
    > field 1: transaction_id
    > field 2: buyer_id
    > field 3: seller_id
    >
    > So, what I'm trying to do is build a single query that will return the
    > usernames of both the buyer and the seller.
    >
    > Your help is greatly appreciated.
    >
    > Ross
    SELECT
    T.transaction_id,
    B.username AS buyer,
    S.username AS seller
    FROM
    Transactions AS T,
    Users AS B,
    Users AS S
    WHERE
    T.buyer_id = B.user_id AND
    T.seller_id = S.user_id

    Notes:
    1. Your table names are dangerously close to the reserved words
    TRANSACTION and USER. Please see [url]www.aspfaq.com/2080[/url] for a list of
    reserved words.

    2. In the future please specify the what database and version you are
    using.


    HTH
    -Chris Hohmann




    Chris Hohmann Guest

  5. #4

    Default Re: many-to-many query -- buyers and sellers

    Thank you very much for the help. And I appologize for the missing
    details of my question. I am using SQL Server. Also, my table names in
    the original post are not actually the ones in my real application. I
    used those names only for illustration purposes.

    Thanks again for the help! I now understand where I was going wrong
    with my inner joins.

    Ross
    Ross Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139