sql >> データベース >  >> RDS >> Mysql

異なる列と2つのテーブルからデータを選択します

    JOINだけ このテーブルで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;
    

    更新されたSQLフィドルデモ

    | 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. クエリがMySqlのWritingtonetの状態でスタックするのはなぜですか?

    2. mysqlのhtml入力からデータを検索

    3. MySQLで行番号インデックス列を生成(つまり書き込み)します

    4. MySQLLIKE制限