sql >> Database >  >> RDS >> Mysql

SQL-query om verkopers en kopers te matchen

Het antwoord van @SérgioMichels zou correct moeten zijn. Maar het krijgt niet de naam van de verkoper en het gebruikt syntaxis die (naar mijn mening) vermeden moet worden.

Dus hier is een alternatief...

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 = ?


  1. CASE-instructie in SQL WHERE-clausule

  2. Kenmerken laden van gekoppelde modellen met sequelize.js

  3. Hoe weeknummer in sql . te extraheren

  4. Inleiding tot PL/SQL-referentiecursors in Oracle Database