【问题标题】:Echo a MySQL table with a column derived from two other columns and a second table回显一个 MySQL 表,其中一列派生自其他两列和第二个表
【发布时间】:2011-12-16 12:56:51
【问题描述】:

我想显示订单表中的几列,其中一个是派生出来的,显示英镑订单价格,该价格已使用另一个表中的汇率转换。汇率本身取决于订单表中的另外两列。

这两个表是:

TABLE `orders`
`id` 
`customer` 
`season_id`
`order_id`
`style_id`
`quantity`
'currency'
`order_price`

TABLE `forex`
`id`
`currency`
`season_id` 
`rate`

currency | season_id | rate 
 USD     |  W2012    | 1.5600   
 USD     |  S2012    | 1.5100   
 EUR     |  W2012    | 1.1510       
 EUR     |  S2012    | 1.1800       
 CAD     |  S2012    | 1.4600   
 CAD     |  S2012    | 1.5010       

我需要使用适当的汇率(外汇表中的汇率)将 order_price(订单表)转换为英镑订单价格,该汇率取决于货币类型(例如美元、欧元 - 订单表中的货币)并随季节而变化(例如,冬季、春季 - 订单表中的 season_id)

因此,订单表在浏览器中的回显应该类似于:

customer|season_id  |style_id|quantity|currency|order_price|rate    |price GBP|
USA     |W2012      |6200    |2,550   |USD     |35,000     |1.5600  |22,436   |
Germany |W2012      |6200    |3,000   |EUR     |45,000     |1.1510  |39,096   |
Germany |S2012      |7220    |5,000   |EUR     |55,000     |1.1650  |47,210   |

因此该语句将类似于:显示 exchange_rate 表中的汇率,该汇率对应于 orders 表中的 season_id 和货币。

如何在选择语句中获得正确的汇率,以便能够在其他列旁边显示英镑订单价格?

对于混乱的解释道歉,在这个领域仍然很绿色,但男孩我想了解更多!

谢谢, 德里克。

【问题讨论】:

    标签: mysql


    【解决方案1】:
    SELECT o.customer, 
    o.season_id, 
    o.style_id, 
    o.quantity, 
    o.currency
    o.order_price, 
    o.rate,  
    round(o.order_price/f.rate) as "price GBP"  <-- assuming you need a round figure
    from orders as o
    inner join forex as f
    on o.season_id = f.season_id
    and o.currency = f.currency
    

    在此之间,mysql没有原生支持货币格式12,300
    如果你需要,它需要一些额外的处理来格式化字符串/数字。

    【讨论】:

    • 感谢 ajreal 的快速回复,以及别名提示 - 查看长公式时可以节省时间并且更容易!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多