sql >> Database teknologi >  >> RDS >> Mysql

SQL-forespørgsel til at matche sælgere og købere

@SérgioMichels' svar burde være korrekt. Men det får ikke sælgerens navn, og det bruger syntaks, der (efter min mening) bør undgås.

Så her er et alternativ...

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. 2PL, streng vs streng model, er der nogen fordel?

  2. Hvordan genererer jeg en CREATE TABLE-sætning for en given tabel i SQL Server?

  3. Forårsaget af:android.database.sqlite.SQLiteException:ingen sådan tabel:BOG (kode 1 SQLITE_ERROR)

  4. MySQL - VÆLG og OPDATER