MySQL:在订购新字段之前加入3个表并将价格乘以货币

时间:2016-10-15 16:52:17

标签: mysql join

我有3个表(简化如下),我想加入并将bed.price与currency.oneDollar相乘,然后以新的字段priceInDollar的顺序排序。

酒店

touches

hotelID   city             name         
2         London           Hotel-London-Inn                    

货币

bedID     hotelID          room            price     cur
1         2                single room     10        USD
1         2                double room     100       MXN

这就像我想要的那样加入酒店和床,但无法弄清楚如何将价格乘以美元作为一个新领域并订购。

id        cur              oneDollar         
1         USD              1                     
2         MXN              0.052605  

1 个答案:

答案 0 :(得分:1)

You need another join in the sample you get all the currency value but you can add more where condition for filter

     SELECT 
          hotel.*
        , bed.*
        , bed.price
        , currency.cur
        , bed.price * currency.oneDollar as priceInDollar
        FROM hotel 
        INNER JOIN bed ON hotel.hotelID = bed.hotelID
        INNER JOIN currency on currency.cur = bed.cur 
        WHERE hotel.city = 'London' 
        ORDER BY bed.price * currency.oneDollar  ASC