Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>SELECT * FROM ( SELECT t1.id_auction, t2.bidder_1 AS bidder, t2.item 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 FROM table1 t1 INNER JOIN table1 t2 ON t1.id_auction = t2.id_auction AND t2.winner_2 = 1 ) AS sub ORDER BY id_auction; </code></pre> <h2><a href="http://www.sqlfiddle.com/#!2/1990e/3" rel="nofollow">SQL Fiddle Demo</a></h2> <p>This will give you:</p> <pre><code>| ID_AUCTION | BIDDER | ITEM | ------------------------------- | 1 | Alice | cup | | 1 | Ben | cup | | 2 | Alice | mug | | 3 | Charles | pen | | 3 | Ben | pen | </code></pre> <hr> <h2>Update 1</h2> <p>Just select a new column <code>bidder_number</code> with value 1 in the first query, and 2 in the second one like this:</p> <pre><code>SELECT * FROM ( SELECT t1.id_auction, t2.bidder_1 AS bidder, t2.item, 1 AS bidder_number -- &lt;---------- This is the new column 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 -- &lt; --------- with value 2 for bidders 2 FROM table1 t1 INNER JOIN table1 t2 ON t1.id_auction = t2.id_auction AND t2.winner_2 = 1 ) AS sub ORDER BY id_auction, bidder; </code></pre> <p>This will give you:</p> <pre><code>| ID_AUCTION | BIDDER | ITEM | BIDDER_NUMBER | ----------------------------------------------- | 1 | Alice | cup | 1 | | 1 | Ben | cup | 2 | | 2 | Alice | mug | 2 | | 3 | Ben | pen | 1 | | 3 | Charles | pen | 2 | </code></pre> <h2><a href="http://www.sqlfiddle.com/#!2/1990e/8" rel="nofollow">SQL Fiddle Demo</a></h2>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload