LarsJK
LarsJK

Reputation: 2196

SQL query to match sellers and buyers

Okay so i got one table with people. One table with items that are given away and one with items people want.

People:
Person_ID, Name

Giveaways:
Person_ID, Item_ID

Wishlist:
Person_ID, Item_ID

So i want a query that returns exchange suggestions for a certain user.

So if i want exchange suggestions for person A it should return a list of people that are giving away an item that person A wants, and want an item that person A is giving away. The result should include: Person A's item, name of person too make exchange with as well as id and item_ID.

Upvotes: 3

Views: 1848

Answers (3)

MatBailie
MatBailie

Reputation: 86716

@SérgioMichels' answer should be correct. But it doesn't get the seller's name, and it uses syntax that should (in my opinion) be avoided.

So, here is an alternative...

SELECT
  buyer.name          AS buyer,
  buyerWants.name     AS buyer_wants,      (assuming the items have names),
  buyerHas.name       AS buyer_has,
  seller.name         AS seller,
  sellerWants.name    AS seller_wants,
  sellerHas.name      AS seller_has
FROM
  People              AS buyer
INNER JOIN
  Wishlist            AS buyerWants
    ON buyerWants.person_id = buyer.person_id
INNER JOIN
  Giveaways           AS sellerHas
    ON sellerHas.item_id = buyerwish.item_id
INNER JOIN
  People              AS seller
    ON seller.person_id = sellerHas.seller_id
INNER JOIN
  WishList            AS sellerWants
    ON sellerWants.person_id = seller.person_id
INNER JOIN
  GiveAways           AS buyerHas
    ON  buyerHas.item_id = sellerWants.item_id
    AND buyerHas.person_id = buyer.person_id
WHERE
  buyer.person_id = ?

Upvotes: 1

DRapp
DRapp

Reputation: 48139

select
      AllGiveWish.WhichWay,
      AllGiveWish.Item_ID,
      p1.Name as MainPersonName,
      p2.Name as OtherPersonName
   from
      ( select   
              "Give" as WhichWay,
              G.Item_ID,
              G.Person_ID as MainPerson,
              W.Person_ID as OtherPerson
           from
              GiveAways G
                 JOIN WishList W
                    on G.Item_ID = W.Item_ID
           where
              G.Person_ID = YourSinglePersonParm 
        UNION ALL
        select   
              "Wish" as WhichWay,
              W.Item_ID,
              W.Person_ID as MainPerson,
              G.Person_ID as OtherPerson
           from
              WishList W
                 JOIN GiveAways G
                    on W.Item_ID = G.Item_ID
           where
              W.Person_ID = YourSinglePersonParm ) As AllGiveWish

      join People P1
            on AllGiveWish.MainPerson = P1.Person_ID

      join People P2
            on AllGiveWish.OtherPerson = P2.Person_ID

Upvotes: 0

user800014
user800014

Reputation:

I think that what you need is something like this:

select p.*
     , w.item_id  item_wanted
     , g.person_id  person_giveaway
  from People p
     , Wishlist w
     , Giveaways g
 where p.person_id = ?
   and p.person_id = w.person_id
   and g.person_id != p.person_id
   and g.item_id = w.item_id
   and exists( select 1
                 from Wishlist w1
                    , Giveaways g1
                where g1.person_id = p.person_id
                  and g1.item_id   = w1.item_id
                  and w1.person_id = g.person_id )

Upvotes: 0

Related Questions