sql >> Database >  >> RDS >> Mysql

Selecteer gegevens uit verschillende kolommen en twee tabellen

Gewoon JOIN met deze tabel 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;

Bijgewerkte SQL Fiddle-demo

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


  1. Hoe retourneer ik willekeurige getallen als een kolom in SQL Server 2005?

  2. Wat is Greenplum-database? Inleiding tot de Big Data-database

  3. Waarom het belangrijk is om gebruikerstoegang in uw database te beperken

  4. Hoe kan ik de waarde optellen in de kamerdatabase Android?