Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I join 2 table(Table1 and Table 2) to produce this ? Help
SQL
Table 1
userID | Name
0001   | Tim
0002   | James
0003   | Bond
0004   | Steve
0005   | Bill

Table 2
ProductID | BuyerID | SellerID
00001     | 0001    | ooo4
00002     | 0003    | 0005
00003     | 0001    | 0002

How to make this
ProductID | Buyer | Seller
00001     | Tim     | Steve
00002     | Bond    | Bill
00003     | Tim     | James
Posted

Try this
I've queried this! :)

SQL
-- TABLE_1 (USERID, NAME)
-- TABLE_2 (PRODUCTID, BUYERID, SELLERID)

SELECT A.PRODUCTID, B.NAME, C.NAME
FROM TABLE_2 A
INNER JOIN TABLE_1 B ON A.BUYERID = B.USERID 
INNER JOIN TABLE_1 C ON A.SELLERID = C.USERID
ORDER BY A.PRODUCTID


Hope it helps! :)
 
Share this answer
 
v4
Comments
Maciej Los 19-Jul-13 3:46am    
There are 2 tables...
berrymaria 19-Jul-13 3:49am    
Yes, there are two tables. I'm using Table_1 and Table_2.
Have you tried executing my query?
berrymaria 19-Jul-13 3:50am    
Oh, I thought you're the one asking the question. But yes. I used two tables.
Isn't clear though?
Maciej Los 19-Jul-13 4:03am    
Sorry, my mistake. On the first look, your query was wrong, but now i see it's perfect!
+5!
berrymaria 19-Jul-13 4:06am    
Oh! :( I don't find any wrong with my query. Since I really tested it in the first place. But I added ORDER BY clause though. Thanks for the vote anyways:)
SQL
Select p.ProductID,b.Name as Buyer,s.Name as seller from table2 p,table1 s,table1 b
where p.BuyerID=b.UserID
and p.SellerID=s.UserID
 
Share this answer
 
Comments
Maciej Los 19-Jul-13 3:45am    
Using WHERE as a join instruction is an old practice ;(
Rather than WHERE, use JOIN.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900