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

Seleziona i dati da diverse colonne e due tabelle

Basta JOIN con questa tabella USD :

SELECT sub.*, USD.USD
FROM
(
  SELECT 
    t1.id_auction,
    t2.bidder_1 AS bidder,
    t2.item,
    1 AS bidder_number    
  FROM table1 t1
  INNER JOIN table1 t2  ON t1.id_auction = t2.id_auction
                       AND t2.winner_1 = 1
  UNION ALL
  SELECT 
    t1.id_auction,
    t2.bidder_2 AS bidder,
    t2.item,
    2                    
  FROM table1 t1
  INNER JOIN table1 t2  ON t1.id_auction = t2.id_auction
                       AND t2.winner_2 = 1
) AS sub
INNER JOIN USD ON sub.id_auction = USD.id_auction
ORDER BY id_auction, bidder;

Dimostrazione SQL Fiddle aggiornata

| ID_AUCTION |  BIDDER | ITEM | BIDDER_NUMBER | USD |
-----------------------------------------------------
|          1 |   Alice |  cup |             1 | 100 |
|          1 |     Ben |  cup |             2 | 100 |
|          2 |   Alice |  mug |             2 | 150 |
|          3 |     Ben |  pen |             1 |  50 |
|          3 | Charles |  pen |             2 |  50 |